如何使用 preg_replace 匹配字符串中的字符并在每个字母后放置一个点
How can I match characters in a string and palce a dot after each Letter using preg replace
我的代码有效。但是我希望它 return 下面的结果。
李四 L.R.T.
取而代之的是 returns(它只有在找到 2 个或更多点时才会这样。)
李四 L..r T.
$string = "John Doe l. r t";
$string = preg_replace_callback('/\b\s[A-z]{1}\b/', function ($matches) {
return strtoupper($matches[0]);
}, $string);
echo preg_replace('/\b\s[A-z]{1}\b/', '[=10=].', $string);
尝试匹配被 space 包围的字母,或者在文本开头和结尾被 space 包围的字母:
echo preg_replace("/\s([a-zA-Z])\s|$/", ".", $input_lines);
我对你的代码做了一些修改。
试试这个代码:-
$string = "John Doe l. r t";
$string = preg_replace_callback('/\b\s[A-z]{1}\b/', function ($matches) {
return strtoupper($matches[0]);
}, str_replace('.', '', $string));
echo preg_replace('/\b\s[A-z]{1}\b/', '[=10=].', $string);
我的代码有效。但是我希望它 return 下面的结果。
李四 L.R.T.
取而代之的是 returns(它只有在找到 2 个或更多点时才会这样。) 李四 L..r T.
$string = "John Doe l. r t";
$string = preg_replace_callback('/\b\s[A-z]{1}\b/', function ($matches) {
return strtoupper($matches[0]);
}, $string);
echo preg_replace('/\b\s[A-z]{1}\b/', '[=10=].', $string);
尝试匹配被 space 包围的字母,或者在文本开头和结尾被 space 包围的字母:
echo preg_replace("/\s([a-zA-Z])\s|$/", ".", $input_lines);
我对你的代码做了一些修改。
试试这个代码:-
$string = "John Doe l. r t";
$string = preg_replace_callback('/\b\s[A-z]{1}\b/', function ($matches) {
return strtoupper($matches[0]);
}, str_replace('.', '', $string));
echo preg_replace('/\b\s[A-z]{1}\b/', '[=10=].', $string);