PHP过滤黑名单关键字的例子
关键字过滤非常的简单把要过滤的内容存在数组或文档中,然后用户提交时我们进行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('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 "<pre class="brush:php;toolbar:false">"; //var_dump($w); //$words = "aaa|bbb|ccc|ddd|eee"; $matched = preg_replace('/'.$word.'/i', '***', $string); return $matched; //开源代码phprm.com } $content = "<a href='#'>我要aaafsdfbbb</a>"; if ($result = Filter_word($content, './words.txt') ){ 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
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article
How to fix KB5055523 fails to install in Windows 11?
4 weeks ago
By DDD
How to fix KB5055518 fails to install in Windows 10?
4 weeks ago
By DDD
Roblox: Grow A Garden - Complete Mutation Guide
3 weeks ago
By DDD
Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How to fix KB5055612 fails to install in Windows 10?
3 weeks ago
By DDD

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
Java Tutorial
1664
14


CakePHP Tutorial
1423
52


Laravel Tutorial
1317
25


PHP Tutorial
1268
29


C# Tutorial
1243
24

