Why does ucwords () behave like ucfirst ()?

3

I have an array of people from which I want the first letter of each word to become capital, so I am using ucwords() . The problem arises since you are acting as if you are using ucfirst() .

Code:

foreach ($data['alumnos'] as &$alumno) {
  var_dump($alumno['nombre_completo']);
  $alumno['nombre_completo'] = ucwords(strtolower($alumno['nombre_completo']));
  var_dump($alumno['nombre_completo']);
}

Exit:

string(29) "BARRIOS HERNANDEZ CHRISTIAN"
string(29) "Barrios hernandez christian"
string(32) "CASTAÑEDA PADILLA JUAN DIEGO"
string(32) "CastaÑeda padilla juan diego"
string(31) "CORONA VALADEZ CARLOS ALEXIS"
string(31) "Corona valadez carlos alexis"
string(31) "DE REZA VELEZ JESUS OSWALDO"
string(31) "De reza velez jesus oswaldo"
string(34) "ALTAMIRANO MORELOS DAN EMMANUEL"
string(34) "Altamirano morelos dan emmanuel"
string(34) "ALVARADO CONTRERAS JORGE CARLOS"
string(34) "Alvarado contreras jorge carlos"

Only convert the first letter as if it were ucfirst() . In which part I am implementing it in the wrong way or where the problem arises.

It works well for me when I do it directly in a one-person arrangement.

Example:

$data['profesor']['nombre_completo'] = ucwords(strtolower($data['profesor']['nombre_completo']));
    
asked by Eduardo Javier Maldonado 06.10.2018 в 00:06
source

1 answer

4

I was testing your code and just change the ucwords by mb_convert_case , since the latter also transforms you once with accented letters and accents to lowercase as the Ñ ; you can see your example by running here with mb_convert_case , I hope it's what you're looking for.

Example of mb_convert_case:

<?php
  $str = "mary had a Little lamb and she loved it so";
  $str = mb_convert_case($str, MB_CASE_UPPER, "UTF-8");
  echo $str; // Muestra MARY HAD A LITTLE LAMB AND SHE LOVED IT SO
  $str = mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
  echo $str; // Muestra Mary Had A Little Lamb And She Loved It So
?>

The mb_convert_case has 3 parameters:

1) str: The string to be converted.

2) mode: The conversion mode. It can be MB_CASE_UPPER, MB_CASE_LOWER, or MB_CASE_TITLE.

3) encoding: The encoding parameter is the character encoding. If it is omitted, the value of the internal character encoding will be used.

    
answered by 06.10.2018 / 00:19
source