String替换特殊字符之间的单词

时间:2017-11-04 21:17:56

标签: php regex str-replace

$text_string = "One One One,One.One OneTwo, Onetwo .Onetwo TwoOne One";

我想替换“#34; One"与"三"单独或在特殊字符之间或在字符串的开头/结尾处。 有人有想法吗?

结果必须是:

$text_string = "Three Three Three,Three.Three OneTwo, Onetwo .Onetwo TwoOne Three";

1 个答案:

答案 0 :(得分:2)

您可以使用\b检查字边界:

$str = 'One One One,One.One OneTwo, Onetwo .Onetwo';
$replaced = preg_replace('/\bOne\b/', 'Three', $str);
echo $replaced; // Three Three Three,Three.Three OneTwo, Onetwo .Onetwo TwoOne Three