You Are Here Home > A PHP Competition

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.

A PHP Competition
Filed under: Fun, PHP, Programming   Posted by: Codehead

Got a Question?

Get answers here.

9 Comments »

  1. chris:
     

    str_replace(‘ ‘,’_',ucwords(str_replace(‘_’,’ ‘,’Lorem_ipsum_dolor_sit_amet,_consectetur_adipisicing_elit’)));

    Comment

  2. Codehead:
     

    Chris, your version is great but what if there was a space in one the words?

    Comment

  3.  

    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

  4. Codehead:
     

    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

  5.  

    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

  6.  

    I came up with a better solution:

    echo preg_replace(‘/_(\w)/e’,”_.strtoupper(\\1)”,’Lorem_ipsum_dolor_sit amet,_consectetur_adipisicing_elit’);

    Comment

  7. chris:
     

    echo(str_replace(Array(” “,”|”),Array(“_”,” “),ucwords(str_replace(Array(” “,”_”),Array(“|”,” “),”Lorem_ipsum_dolor_sit amet,_consectetur_adipisicing_elit”))));

    Comment

  8. chris:
     

    ^ oops, i didn’t realize Boban posted the same solution :)

    Comment

  9. Codehead:
     

    That’s fine Chris, good solutions, hoping to see more :)

    Comment

RSS feed for comments on this post. TrackBack URL

Leave a comment