-1

当我的搜索值存储在字符串数组中时,如何在字符串中进行搜索和替换?

$strFromSearchBox = 'Why you foo, bar, I ought to tar you';

$theseWords       = array('foo', 'bar', 'tar');

所以,我想foo bar tar用空格替换字符串。是否可以不使用whileorfor循环?

4

3 回答 3

1

str_replace接受数组作为参数。

$output = str_replace($theseWords, ' ', $strFromSearchBox);
于 2013-01-01T13:19:00.270 回答
1

str_replace接受数组!

str_replace(array(
    'foo',
    'bar',
    'tar'
), '', 'Why you foo, bar, I ought to tar you');
于 2013-01-01T13:19:00.930 回答
0

你正在寻找str_replace

$string = 'Why you foo, bar, I ought to tar you';
$result = str_replace(
   array( 'foo', 'bar', 'tar'), 
   '', 
   $string
);
于 2013-01-01T13:21:58.413 回答