Home » PHP/MySQL, Programming Techniques » Convert under_scored strings to CamelCased without RegEx in PHP


Developers normally use functions that use Regular Expression to convert under_scored strings in to CamelCased strings. Usage of Regular Expression is quite heavy. We can use a simple yet powerful method to convert these strings to CamelCase without using RegEx in PHP.

Here is the method:

echo join(””, array_map(“ucwords”, explode(‘_’, $under_scored)));

This combination of functions, first explodes the underscored string in to an array containing sub strings formed by “_” delimiter and applies ucwords() function to each member of the array. ucwords() uppercases the first character of the string. These uppercased strings are again joined back to form a single string which is the CamelCased version of the actual string.

Please let me know if you have any questions.


  1. Sverri says:

    If you want the first word to be ignored you can use a function like this:

    function under2camel($string) {
    $words = explode(‘_’, $string);
    $first = array_shift($words);
    foreach ($words as &$val) $val = ucwords($val);
    return $first . implode(”, $words);

    echo under2camel(‘underscored_function_name’);

  2. Aneeska says:

    Wonderful! Thanks Sverri!


Leave a Reply

Page optimized by WP Minify WordPress Plugin