Forum Moderators: coopster

Message Too Old, No Replies

Capitalize first letter of each word in a string

         

phpshift

8:35 pm on Apr 19, 2007 (gmt 0)

10+ Year Member



I wrote a simple function called "ucallfirst()" in order to capitalize the first letter of each word in a string. Anyway, here it is:

function ucallfirst($string)
{
$string=explode(" ",$string);
$i=0;
while($i<count($string))
{$string[$i]=ucfirst($string[$i]);
$i++;}
return implode(" ",$string);
}

And it's as simple as that. :)

mooger35

9:03 pm on Apr 19, 2007 (gmt 0)

10+ Year Member



doesn't ucwords [ca3.php.net] do that?

phpshift

2:17 am on Apr 20, 2007 (gmt 0)

10+ Year Member



Not in versions earlier than 4.0. Odds are almost infinitely slim you'll have that problem though. I just wrote it because I saw an old post earlier asking about this and there was no solution to that. I suppose the makers of PHP got to it. :D

eelixduppy

2:22 am on Apr 20, 2007 (gmt 0)



There are a couple other alternate solutions posted at the ucwords documentation in the user-contributed notes. Mostly for languages other than english, though.

Thanks for your solution, phpshift, and Welcome to WebmasterWorld! :)

coopster

6:53 pm on Apr 20, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You could use CSS to do it too.
[w3.org...]