Page 1 of 1

Make First letter uppercase and the rest lowercase

Posted: Mon Aug 11, 2014 9:40 pm
by donny
hello

can somebody please help me

I have 2 fields $firstname and $lastname

I want to make a $fullname which is basically $firstname + $lastname but i want the format of the name to be like this

John Doe

and if the last name has a ' I want the letter after the ' to capitalize also

John D'Angelo

thank you

Re: Make First letter uppercase and the rest lowercase

Posted: Mon Aug 11, 2014 10:08 pm
by Celauran

Re: Make First letter uppercase and the rest lowercase

Posted: Mon Aug 11, 2014 10:41 pm
by donny
I found this code online can somebody please edit it so it will work with my variables please.

my name variable is $first_last_name
if possible i want to create a variable $first_last_name_edited to be the fixed name

Code: Select all

//FUNCTION

function ucname($string) {
    $string =ucwords(strtolower($string));

    foreach (array('-', '\'') as $delimiter) {
      if (strpos($string, $delimiter)!==false) {
        $string =implode($delimiter, array_map('ucfirst', explode($delimiter, $string)));
      }
    }
    return $string;
}

//TEST

$names =array(
  'JEAN-LUC PICARD',
  'MILES O\'BRIEN',
  'WILLIAM RIKER',
  'geordi la forge',
  'bEvErly CRuSHeR'
);
foreach ($names as $name) { print ucname("{$name}\n"); }

thank you very much

Re: Make First letter uppercase and the rest lowercase

Posted: Mon Aug 11, 2014 11:38 pm
by donny
i got it.

thanks anyway