Make First letter uppercase and the rest lowercase

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Make First letter uppercase and the rest lowercase

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Make First letter uppercase and the rest lowercase

Post by Celauran »

donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Make First letter uppercase and the rest lowercase

Post 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
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Make First letter uppercase and the rest lowercase

Post by donny »

i got it.

thanks anyway
Post Reply