Home php教程 php手册 PHP过滤黑名单关键字的例子

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

Jun 02, 2016 am 09:13 AM

关键字过滤非常的简单把要过滤的内容存在数组或文档中,然后用户提交时我们进行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


教程链接:

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1243
24