我使用preg_replace($oldWords, $newWords, $string);
来替换单词数组。preg用PHP中的部分模式替换完整的单词
我希望替换开始foo
所有单词为hello
,所有的话开始bar
为world
即foo123
应更改为hello
,foobar
应更改为hello
,barx5
应更改为world
等
如果我的数组被定义为:
$oldWords = array('/foo/', '/bar/');
$newWords = array('hello', 'world');
然后foo123
更改为hello123
而不是hello
。同样barx5
更改为worldx5
而不是world
如何替换完整的匹配单词?
谢谢。