如何使用 PHP preg_replace 替换字符串中的特定子字符串 [英] How to replace particular Substring within a String using PHP preg_replace
本文介绍了如何使用 PHP preg_replace 替换字符串中的特定子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 preg_replace
替换字符串中的单词或单词序列?例如:
I am trying to replace word or sequence of words in a string using preg_replace
?
For example:
更改这些:
ABCExample to abcExample
AnotherExample to anotherExample
XyzAbcExample to xyzAbcExample
保持原样:
xyzExample to xyzExample
new to new
推荐答案
需要使用preg_replace_callback
函数.
$str = <<<EOT
ABCExample
AnotherExample
XyzAbcExample
xyzExample
new
EOT;
echo preg_replace_callback('~(?m)^[A-Za-z]*?(?=(?:[A-Z][a-z]+)+$)~', function ($m)
{
return strtolower($m[0]);
}, $str);
这篇关于如何使用 PHP preg_replace 替换字符串中的特定子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文