一个词语过滤的函数_PHP教程
程序员文章站
2024-04-05 07:59:23
...
function words($fileName,$word){
if (!file_exists($fileName))
{
return FALSE;
}
$badWords = file($fileName);
$word = trim($word);
$word = preg_replace(/d/,,$word);
$word = preg_replace(/[a-zA-Z]/,,$word);
foreach ($badWords as &$v){
$v=trim($v);
}
if (in_array($word,$badWords)){
return TRUE;
}else{
foreach ($badWords as $vv){
if (strpos($word,$vv) !== false){
return TRUE;
}
}
return FALSE;
}
}
参数 :$fileName 要过滤的文本文件,每行一个;
$word 需要过滤的词;
返回值:TRUE表示有敏感词 ,FALSE为没敏感字
if (!file_exists($fileName))
{
return FALSE;
}
$badWords = file($fileName);
$word = trim($word);
$word = preg_replace(/d/,,$word);
$word = preg_replace(/[a-zA-Z]/,,$word);
foreach ($badWords as &$v){
$v=trim($v);
}
if (in_array($word,$badWords)){
return TRUE;
}else{
foreach ($badWords as $vv){
if (strpos($word,$vv) !== false){
return TRUE;
}
}
return FALSE;
}
}
参数 :$fileName 要过滤的文本文件,每行一个;
$word 需要过滤的词;
返回值:TRUE表示有敏感词 ,FALSE为没敏感字