如何用HTML标签包含每个单词中的第一个字母?

Tim*_*nen 3 php replace

如何用HTML标签包含每个单词中的第一个字母?

例如:

$string = replace_first_word("some simple text",'<big>{L}</big>');
echo $string; //returns: <big>s</big>ome <big>s</big>imple <big>t</big>ext
Run Code Online (Sandbox Code Playgroud)

编辑:哦,忘了提一个重点,它需要使用UTF-8 Unicode ......因为我打算用这个支持俄语和英语.

Gum*_*mbo 7

试试这个:

$string = preg_replace('/(?:^|\b)(\p{L})(\p{L}*)/u', '<big>$1</big>$2', 'some simple words');
Run Code Online (Sandbox Code Playgroud)

或者如果你想在一个函数中:

function replace_first_word($str, $format) {
    return preg_replace('/(?:^|\b)(\p{L})(\p{L}*)/u', str_replace('{L}', '$1', $format).'$2', $str);
}
Run Code Online (Sandbox Code Playgroud)