Reputation: 696
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
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
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