$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";
答案 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