shihabudheen
shihabudheen

Reputation: 696

convert underscore to camelcaps in php

I want to convert an underscore string into a camelcase'd string, in php. How can this be done using preg_replace?

For example: offer_list to offerList.

Upvotes: 0

Views: 194

Answers (2)

meWantToLearn
meWantToLearn

Reputation: 1700

without preg:

 /**
 * Converts underscore string into camel
 * @param   string    $str
 * @return  string 
 */
public static function underToCamel($str){
    return   \lcfirst(str_replace(' ', "", ucwords(strtr($str, '_-', ' '))));
}

Upvotes: 0

Ian McMahon
Ian McMahon

Reputation: 1700

Can be done using a /e modifier on your regex, like this:

preg_replace("/_([a-zA-Z])/e", 'strtoupper("$1")', "camel_case_word")

Upvotes: 4

Related Questions