当我的搜索值存储在字符串数组中时,如何在字符串中进行搜索和替换?
$strFromSearchBox = 'Why you foo, bar, I ought to tar you';
$theseWords = array('foo', 'bar', 'tar');
所以,我想foo
bar
tar
用空格替换字符串。是否可以不使用while
orfor
循环?
str_replace
接受数组作为参数。
$output = str_replace($theseWords, ' ', $strFromSearchBox);
str_replace
接受数组!
str_replace(array(
'foo',
'bar',
'tar'
), '', 'Why you foo, bar, I ought to tar you');
你正在寻找str_replace
$string = 'Why you foo, bar, I ought to tar you';
$result = str_replace(
array( 'foo', 'bar', 'tar'),
'',
$string
);