关键字过滤非常的简单把要过滤的内容存在数组或文档中,然后用户提交时我们进行preg_match或istrpos判断即可解决了,具体方法如下.
例,代码如下:
-
- function check_($data){
- $keywords = "你是大神吗|你好|去死";
- if(preg_match("/$keywords/i",$data)){
- return;
- }else{
- return $data;
- }
-
- }
关键字的存放形式为txt,txt文件中以这样形式存放:|赌博机|卖血|出售肾|出售器官|眼角膜,代码如下:
- <?php
- function Filter_word( $str, $fileName )
- {
- if ( !($words = file_get_contents( $fileName )) ){
- die('file read error!');
- }
- $str = strtolower($str);
-
- $word = preg_replace("/[1,2,3] | /i", '', $words);
-
-
-
-
-
- $matched = preg_replace('/'.$word.'/i', '***', $string);
- return $matched;
- }
-
- $content = "<a href='#'>我要aaafsdfbbb</a>";
- if ($result = Filter_word($content, './words.txt') ){
- echo $result;
- echo "替换成功 ";
- }else{
- echo "替换失败! ";
- ?>
|