用_字符后面的大写字母替换每个_字符

Lim*_*eni 2 php regex string

我该如何解决这个问题:

我想要像这样转换字符串:

some_words => someWords
some_more_text => someModeText
Run Code Online (Sandbox Code Playgroud)

希望你明白这一点,我需要用X替换每个_x.

我现在有这样的事情:

$string = 'some_words';
$new = preg_replace('/_([a-z])/', strtoupper('$1'), $string);
Run Code Online (Sandbox Code Playgroud)

但这不起作用.我可以在preg_replace函数中使用内置的PHP函数吗?

我该如何解决这个问题?

谢谢!

nic*_*ckb 5

你可以preg_replace_callback()像这样使用:

$new = preg_replace_callback('/_([a-z])/', function( $match) { 
    return strtoupper( $match[1]);
}, $string);
Run Code Online (Sandbox Code Playgroud)