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.
Hamid Alipour is a partner in Codehead, LLP with his wife, Tess. Hamid speaks 12 markup and programming languages [Yes, 12: PHP, CSS, Ajax, JavaScript, HTML/XHTML, Java, Python, C/C++, ASP, Visual Basic, Scheme and Action Script]; has a penchant for solving the unsolvable; an affinity for clean, hand-written code and is a Zend Certified 
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