Capitalize only the first Letter of a string

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
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Capitalize only the first Letter of a string

Post by Live24x7 »

I want to Capitalize onlt the first Letter of a string.
I tried ucfirst($string) but it also capitalizes every word that comes after 'and' & ','.

With ucfirst, what i get is this:
Fifteen thousand, Nine hundred and Fourty-two

where as i want :
Fifteen thousand, nine hundred and fourty-two

Any suggestions ?
Thanks a lot !
:)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Capitalize only the first Letter of a string

Post by Benjamin »

Code: Select all

echo ucfirst(strtolower("Fifteen thousand, Nine hundred and Fourty-two"));
;)
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Re: Capitalize only the first Letter of a string

Post by Live24x7 »

that works perfect thanks a lot :)

but i failed to understand how this worked ... it first lowered the case of entire string and then used ucfirst.
My original string is always all lowercase and i don't understand what affect did strtolower have on the string.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Capitalize only the first Letter of a string

Post by Benjamin »

I think you are missing something, ucfirst should only change the first character. Maybe you are using a different variable or source?
Post Reply