Home > php教程 > php手册 > body text

PHP过滤黑名单关键字的例子

WBOY
Release: 2016-06-02 09:13:41
Original
1535 people have browsed it

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

例,代码如下:

//过滤黑名单关键字函数 

function check_($data){ 
    $keywords = "你是大神吗|你好|去死"; 
    if(preg_match("/$keywords/i",$data)){ 
        return; 
    }else{ 
        return $data; 
    } 
}
Copy after login

关键字的存放形式为txt,txt文件中以这样形式存放:|赌博机|卖血|出售肾|出售器官|眼角膜,代码如下:

<?php 
function Filter_word( $str, $fileName )    
{    
    if ( !($words = file_get_contents( $fileName )) ){    
        die(&#39;file read error!&#39;);    
    }    
    $str = strtolower($str); 
    //var_dump($words); 
    $word = preg_replace("/[1,2,3] | /i", &#39;&#39;, $words); 
    //$wor = substr($word,0,-1); 
    //$w = preg_replace("|/|i", &#39;/&#39;, $word); 
    //echo "<pre class="brush:php;toolbar:false">"; 
    //var_dump($w); 
    //$words = "aaa|bbb|ccc|ddd|eee"; 
        $matched = preg_replace(&#39;/&#39;.$word.&#39;/i&#39;, &#39;***&#39;, $string); 
    return $matched; //开源代码phprm.com 
}    
   
$content = "<a href=&#39;#&#39;>我要aaafsdfbbb</a>";    
if ($result = Filter_word($content, &#39;./words.txt&#39;) ){ 
echo $result; 
    echo "替换成功 ";    
}else{    
    echo "替换失败! ";  
}
Copy after login


教程链接:

随意转载~但请保留教程地址★

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template