A PHP Competition
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.


chris said,
October 28, 2008 @ 5:37 pm
str_replace(’ ‘,’_',ucwords(str_replace(’_',’ ‘,’Lorem_ipsum_dolor_sit_amet,_consectetur_adipisicing_elit’)));
Codehead said,
October 28, 2008 @ 7:44 pm
Chris, your version is great but what if there was a space in one the words?
Boban said,
October 29, 2008 @ 6:06 am
I just modified a bit Cris`s solution:
echo str_replace(` 1 `,`_`,ucwords(str_replace(`_`,` 1 `,`Lorem_ipsum_dolor_sit_amet,_consectetur_adipisicing_elit`)));
Codehead said,
October 29, 2008 @ 6:13 am
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…
Boban said,
October 29, 2008 @ 6:46 am
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’)));
Boban KariĊĦik said,
October 29, 2008 @ 4:01 pm
I came up with a better solution:
echo preg_replace(’/_(\w)/e’,”_.strtoupper(\\1)”,’Lorem_ipsum_dolor_sit amet,_consectetur_adipisicing_elit’);
chris said,
October 29, 2008 @ 4:26 pm
echo(str_replace(Array(” “,”|”),Array(”_”,” “),ucwords(str_replace(Array(” “,”_”),Array(”|”,” “),”Lorem_ipsum_dolor_sit amet,_consectetur_adipisicing_elit”))));
chris said,
October 29, 2008 @ 4:32 pm
^ oops, i didn’t realize Boban posted the same solution
Codehead said,
October 29, 2008 @ 4:56 pm
That’s fine Chris, good solutions, hoping to see more