如何过滤phrase
使用php?谁能给我一个函数调用?
例如,如果我想从句子There are no user contributed notes for this page.
这样句子就像There are user contributed notes for page.
谢谢。
<?php
function filterBadWords($str)
{
$badwords = array('no','this');
$replacements = " ";
foreach ($badwords AS $badword)
{
$str = eregi_replace($badword, str_repeat(' ', strlen($badword)), $str);
}
return $str;
}
$string = "There are user contributed notes for page.";
print filterBadWords($string); // this will filter `no` from `notes`
?>
答案 0 :(得分:2)
$text = preg_replace('/\b(?:no|this)\b ?/i', '', $text);
编辑:
现在它会删除一个空格,如果它在单词后面找到一个空格,那么你最终不会在一行中有两个空格。
$text = 'There are no user contributed notes for this page.';
$text = preg_replace('/\b(?:no|this)\b ?/i', '', $text);
echo $text;
输出:页面有用户提供的注释。
<强>更新强>
如果您想使用数组来更轻松地管理过滤后的单词,您可以这样做:
function filterWords($string){
$bad_words = array('no', 'this');
return preg_replace('/\b(?:'.implode('|', $bad_words).')\b ?/i', '', $string);
}
echo filterWords('There are no user contributed notes for this page.');
答案 1 :(得分:0)
str_replace(array('no', 'this'), '', $text);