I want to put the first letter of each word in uppercase, ignoring the connectors
for example:
$str= mb_convert_case("PLATA JUGUETES Y ÚTILES", MB_CASE_TITLE, "UTF-8");
What I hope to get is: Silver Toys and Tools
I want to put the first letter of each word in uppercase, ignoring the connectors
for example:
$str= mb_convert_case("PLATA JUGUETES Y ÚTILES", MB_CASE_TITLE, "UTF-8");
What I hope to get is: Silver Toys and Tools
In this function that I created, see if the string has more than 2 characters if it is so the first letter will be capitalized:
echo capitalize("PLATA JUGUETES Y ÚTILES");
function capitalize($str, $encoding = 'UTF-8') {
$str = mb_strtolower($str, $encoding);
// Creamos un array de la cádena
$arrStr = explode(' ', $str);
$pushArray = [];
foreach( $arrStr as $value ) {
// Si el string tiene más que 1 character lo convertimos a mayúscula
if ( mb_strlen($value, $encoding) > 1 ) {
$pushArray[] = mb_convert_case($value, MB_CASE_TITLE, $encoding);
}
else {
$pushArray[] = $value;
}
}
// Devolvemos el string completo
return implode(' ', $pushArray); // Plata Juguetes y Útiles
}
I propose an answer that is not mine, I got it from a answer from Antonio Max in SOen . I adapted it since you say that you need the connectors to be the only ones that do not convert to upper case. In the parameter $exceptions
you put the words you do not want to convert. I put all the prepositions, but you can add more words.
<?php
$s = 'PLATA JUGUETES Y ÚTILES PLATA, Juguetes y útiles';
$v = titleCase($s);
echo $v."";
function titleCase($string, $delimiters = array(" ", "-", ".", "'", "O'", "Mc"), $exceptions = array("a","ante","bajo","cabe","con","contra","de","desde","en","entre","hacia","hasta","para","por","según","sin","so","sobre","tras","y"))
{
/*
* Exceptions en minusculas son las palabras que no deseas convertir
* Exceptions en mayusculas son las palabras que no deseas convertir a title case
* pero deben ser converidas a mayusculas, e.g.:
* king henry viii o king henry Viii deben ser King Henry VIII
*/
$string = mb_convert_case($string, MB_CASE_TITLE, "UTF-8");
foreach ($delimiters as $dlnr => $delimiter) {
$words = explode($delimiter, $string);
$newwords = array();
foreach ($words as $wordnr => $word) {
if (in_array(mb_strtoupper($word, "UTF-8"), $exceptions)) {
// check exceptions list for any words that should be in upper case
$word = mb_strtoupper($word, "UTF-8");
} elseif (in_array(mb_strtolower($word, "UTF-8"), $exceptions)) {
// check exceptions list for any words that should be in upper case
$word = mb_strtolower($word, "UTF-8");
} elseif (!in_array($word, $exceptions)) {
// convert to uppercase (non-utf8 only)
$word = ucfirst($word);
}
array_push($newwords, $word);
}
$string = join($delimiter, $newwords);
}//foreach
return $string;
}
?>
In this case, you can use some method to convert the first letters of each word to uppercase but you need to have "y" in lowercase.
You can use ucwords () for words that have a word longer than 2 (" SILVER "," TOYS "," USEFUL ") and for those that do not meet this condition, convert them to lowercase (" Y "," O "," DE "):
$str = mb_convert_case("PLATA JUGUETES Y ÚTILES", MB_CASE_TITLE, "UTF-8");
$words = explode(" ", $str);
$result = "";
foreach ($words as $w) {
if(strlen($w) > 2){
$result.=ucwords(strtolower($w)).' ';
}else{
$result.=strtolower($w).' ';
}
}
echo $result;
in this way you would get as output:
Plata Juguetes y Útiles
You can see the online demo
This also works in the case of accented words in lowercase like:
$str= mb_convert_case("plata juguetes y útiles", MB_CASE_TITLE, "UTF-8");
This is another example:
$str= mb_convert_case("PLATA JUGUETES Y ÚTILES DE JAVIER o jorgesys", MB_CASE_TITLE, "UTF-8");
would have as a resulting value:
Plata Juguetes y Útiles de Javier o Jorgesys