I like to use regular expressions for this because of the simplicity that it provides. The function preg_replace() provides what we need using regular expression.
function nomeProprio($input) {
return preg_replace('/\sd(\ws?)\s/i', ' d$1 ' , mb_convert_case($input, MB_CASE_TITLE, "UTF-8"));
}
$input = 'PORTO DE GALINHAS'; // texto vindo do webservice
echo nomeProprio($input);
the first thing that will happen in the CHICKEN PORT string is that it will go through the mb_convert_case() function that has the MB_CASE_TITLE and UTF-8 parameter that are used to convert the String into Case Title and the charset into UTF-8 respectively. Resulting in:
"Porto De Galinhas"
regular expression will be used in this string
Dissecando a expressão regular: /\sd(\ws?)\s/i
/ // delimitador de início da expressão regular
\s // exige que haja um espaço
d // exige que haja a letra d
( // inicia um grupo de captura para ser utilizado na sequência -> $1
\w // que exige que tenha uma letra(\w), também poderia ser [aeiou]
s? // seguido ou não da letra s
) // finaliza o grupo de captura
\s // mais uma vez exige que haja outro espaço
/ // delimitador de fim da expressão regular
i // faz com que a expressão seja case insensitive considerando o d ou D
that you will find " From ", you will extract only the letter "and" and place it in the capture group $1 causing
" De "
is replaced by
" de "
displaying as final result:
"Porto de Galinhas"
how w can be any letter would find as well
" Da ", " Di ", " Do ", " Du ", " D(qualquer letra) "...
in the above case only the vowels enter the capture group. And also:
" Das ", " Dis ", " Dos ", " Dus ", " D(qualquer letra)s "...
in this other case enter the vowels accompanied by the letter s that will be placed in the place of $1 resulting in:
" das ", " dis ", " dos ", " dus ", " d(qualquer letra)s "...
Note that the substitution will only be in the " Of " part because the rest is already correct with the use of the mb_convert_case function.
Here’s a question with answers that can help a lot of people :)
– Marcos Vinicius
@Marcosvinicius this is good when registering things in DB, just have to be careful in some situations. For example, Rua XV de Novembro will be a problem. But for most cases, it works. If one thinks a little adapts easy easy logic to take care of the Romans (and put everything in upper).
– Bacco
Okay, so not what I meant is to take the
strtolower
from within theforeach
, http://ideone.com/6zHiGe– Guilherme Nascimento
@Guilhermenascimento in the end, since it was to optimize appealed :D
– Bacco
Worth a +100 :D
– Guilherme Nascimento
properCase
is a name that only philosophers give their functions ;)– Wallace Maxters
@Wallacemaxters hope this isn’t bad :P
– Bacco
No, it’s not bad. I like to throw the names in English too, rsrsrsrs
– Wallace Maxters
But if the user puts more than one space?
– Luhhh
@Luhhh Has more than one space on the way outUé (but it works the same way :) . The question does not ask to change the spaces. The code preserves as it was typed.
– Bacco
@Luhhh if you want to remove double spaces, there are several ways. can use replace, explode with array_filter, preg_replace, depends on what is convenient for your specific code.
– Bacco