OK, here is the competition:
Write the shortest piece of PHP code to convert:
Lorem_ipsum_dolor_sit amet,_consectetur_adipisicing_elit
Into this:
Lorem_Ipsum_Dolor_Sit amet,_Consectetur_Adipisicing_Elit
There is no prize other than I will write a post about you ![]()
Leave a comment.
Update:
Chris wrote a perfectly working line of code for the first string I posted, but you will have to write one that will still work if there is a space in one of the words. I fixed the example.

I'm a programmer at 
str_replace(‘ ‘,’_',ucwords(str_replace(‘_’,’ ‘,’Lorem_ipsum_dolor_sit_amet,_consectetur_adipisicing_elit’)));
Comment
Chris, your version is great but what if there was a space in one the words?
Comment
I just modified a bit Cris`s solution:
echo str_replace(` 1 `,`_`,ucwords(str_replace(`_`,` 1 `,`Lorem_ipsum_dolor_sit_amet,_consectetur_adipisicing_elit`)));
Comment
Boban, if you look closer, it has a space in the middle of it:
Lorem_ipsum_dolor_sit amet,_consectetur_adipisicing_elit
amet, shouldn’t be capitalized:
Lorem_Ipsum_Dolor_Sit amet,_Consectetur_Adipisicing_Elit
Yours will capitalize amet…
Comment
I`m sorry, it is completely my fault, i didn’t see that. Here is a solution:
$arr1 = array(‘ 2′,’ 1 ‘);
$arr2 = array(‘ ‘,’_');
echo str_replace($arr1,$arr2,ucwords(str_replace($arr2,$arr1,’Lorem_ipsum_dolor_sit amet,_consectetur_adipisicing_elit’)));
Comment
I came up with a better solution:
echo preg_replace(‘/_(\w)/e’,”_.strtoupper(\\1)”,’Lorem_ipsum_dolor_sit amet,_consectetur_adipisicing_elit’);
Comment
echo(str_replace(Array(” “,”|”),Array(“_”,” “),ucwords(str_replace(Array(” “,”_”),Array(“|”,” “),”Lorem_ipsum_dolor_sit amet,_consectetur_adipisicing_elit”))));
Comment
^ oops, i didn’t realize Boban posted the same solution
Comment
That’s fine Chris, good solutions, hoping to see more
Comment
How about this:
$string = ‘Lorem_ipsum_dolor_sit amet,_consectetur_adipisicing_elit’;
$words = explode(‘_’,$string);
foreach($words as $word) {
$wordarray[] = ucfirst($word).’_';
}
$string = rtrim(implode($wordarray),’_');
echo $string;
Comment
echo preg_replace(‘/_([a-z])/e’, ‘strtoupper(“_$1″)’, ‘Lorem_ipsum_dolor_sit amet,_consectetur_adipisicing_elit’) . “\n”;
Comment
$s1 = ‘Lorem_ipsum_dolor_sit amet,_consectetur_adipisicing_elit’;
$ss = str_split($s1);foreach($ss as $k=>$v){if($v==’_'){$ss[$k+1]=strtoupper($ss[$k+1]);}}
echo implode(”,$ss);
Comment
Not sure how old this post is but thought I would chip in with my little suggestion:
echo mb_convert_case(‘Lorem_ipsum_dolor_sit amet,_consectetur_adipisicing_elit’,MB_CASE_TITLE,’UTF-8′);
Comment