$str = "aa_aa
bb_cc
dd_ef";
$replacement = "camelize('\$1')";
//注意修饰符e
$res = preg_replace("/(.*_.*)/e", $replacement, $str);
echo $res,"\n";
/**
* 下划线转驼峰
* 思路:
* step1.原字符串转小写,原字符串中的分隔符用空格替换,在字符串开头加上分隔符
* step2.将字符串中每个单词的首字母转换为大写,再去空格,去字符串首部附加的分隔符.
*/
function camelize($uncamelized_words,$separator='_')
{
$uncamelized_words = $separator. str_replace($separator, " ", strtolower($uncamelized_words));
return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator );
}
输出:
aaAa
bbCc
ddEf