我想在php中将下划线字符串转换为camelcase字符串。使用preg_replace
如何做到这一点?
例如:offer_list
到offerList
。
可以在正则表达式上使用/e修饰符,如下所示:
preg_replace("/_([a-zA-Z])/e", 'strtoupper("$1")', "camel_case_word")
无preg:
/**
* Converts underscore string into camel
* @param string $str
* @return string
*/
public static function underToCamel($str){
return 'lcfirst(str_replace(' ', "", ucwords(strtr($str, '_-', ' '))));
}