Home  >  Article  >  Backend Development  >  PHP过滤黑名单关键字的方法_php技巧

PHP过滤黑名单关键字的方法_php技巧

WBOY
WBOYOriginal
2016-05-16 20:29:361721browse

本文实例讲述了PHP过滤黑名单关键字的方法。分享给大家供大家参考。具体实现方法如下:

关键字过滤非常的简单把要过滤的内容存在数组或文档中,然后用户提交时我们进行preg_match或istrpos判断即可解决了,具体方法如下.

实例代码如下:

复制代码 代码如下:
//过滤黑名单关键字函数
function check_($data){
 $keywords = "你是大神吗|你好|去死";
 if(preg_match("/$keywords/i",$data)){
  return;
 }else{
  return $data;
 }
}

关键字的存放形式为txt,txt文件中以这样形式存放:|赌博机|卖血|出售肾|出售器官|眼角膜,代码如下:
复制代码 代码如下:
function Filter_word( $str, $fileName )   
{   
    if ( !($words = file_get_contents( $fileName )) ){   
        die('file read error!');   
    }   
    $str = strtolower($str);
//var_dump($words);
$word = preg_replace("/[1,2,3] | /i", '', $words);
//$wor = substr($word,0,-1);
//$w = preg_replace("|/|i", '/', $word);
//echo "
"; <br>
//var_dump($w); <br>
//$words = "aaa|bbb|ccc|ddd|eee"; <br>
    $matched = preg_replace('/'.$word.'/i', '***', $string); <br>
return $matched;<br>
}    <br>
   <br>
$content = "<a href="#">我要aaafsdfbbb</a>";    <br>
if ($result = Filter_word($content, './words.txt') ){ <br>
echo $result; <br>
    echo "替换成功 ";    <br>
}else{    <br>
    echo "替换失败! "; <br>
?>

希望本文所述对大家的PHP程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn