You Are Here Home > A PHP Competition
Resource Index Web Directory

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
Do you have any questions? ask here.




13 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

  10.  

    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

  11. GK:
     

    echo preg_replace(‘/_([a-z])/e’, ‘strtoupper(“_$1″)’, ‘Lorem_ipsum_dolor_sit amet,_consectetur_adipisicing_elit’) . “\n”;

    Comment

  12. Baz:
     

    $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

  13.  

    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

RSS feed for comments on this post. TrackBack URL

Leave a comment