Home > OS >  ucwords(strtolower()) not working together php
ucwords(strtolower()) not working together php

Time:10-30

I have code where I am extracting a name from a database, trying to reorder the word, and then changing it from all uppercase to word case. Everything I find online suggests my code should work, but it does not... Here is my code and the output:

$subjectnameraw = "SMITH, JOHN LEE";
$subjectlname = substr($subjectnameraw, 0, strpos($subjectnameraw, ",")); // Get the last name 
$subjectfname = substr($subjectnameraw, strpos($subjectnameraw, ",")   1) . " "; // Get first name and middle name
$subjectname = ucwords(strtolower($subjectfname . $subjectlname)); // Reorder the name and make it lower case / upper word case

However, the output looks like this:

John Lee smith

The last name is ALWAYS lowercase no matter what I do. How can I get the last name to be uppercase as well?

CodePudding user response:

The above code gives wrong results when there are multibyte characters in the names like RENÉ. The following solution uses the multibyte function mb_convert_case.

$subjectnameraw = "SMITH, JOHN LEE RENÉ";
list($lastName,$firstnName) = explode(', ',mb_convert_case($subjectnameraw,MB_CASE_TITLE,'UTF-8'));
echo $firstnName." ".$lastName;

Demo : https://3v4l.org/ekTQA

  •  Tags:  
  • php
  • Related