PHP解决方案@黑名单过滤
2015-07-21 19:14
826 查看
方案解决目标:对一些黑名单进行过滤处理
输出:
function is_spam($text, $file, $split =':', $regex =false){ $handle = fopen($file,'rb'); $contents = fread($handle, filesize($file)); fclose($handle); $lines = explode("n", $contents); $arr = array();foreach($lines as $line){ list($word, $count)= explode($split, $line);if($regex){ $arr[$word]= $count; }else{ $arr[preg_quote($word)]= $count; }} preg_match_all("~".implode('|', array_keys($arr))."~", $text, $matches); $temp = array();foreach($matches[0]as $match){if(!in_array($match, $temp)){ $temp[$match]= $temp[$match]+1;if($temp[$match]>= $arr[$word]) { returntrue; } }}returnfalse; } $file ='spam.txt'; $str ='This string has cat, dog word';if(is_spam($str, $file)) { echo 'this is spam';}else{ echo 'this is not spam';}
输出:
ab:3dog:3cat:2monkey:2
相关文章推荐
- php const和define的区别(php类中如何定义常量)
- 关于如何改变thinkphp中自定义的成功界面、错误界面、异常界面
- phpcms 系统安装
- ThinkPHP上传文件总结
- 代码上线--php
- A+B for Input-Output Practice (II)
- php-wamp环境搭建
- PHP中const和define()定义常量的细节区别
- 源码安装PHP报错:error: xml2-config not found.
- thinkphp网站缓存写入失败
- thinkphp自动映射分析
- php基础学习笔记
- php后台管理员权限相关表结构
- 常用PHP框架功能对比表
- 成为一名PHP专家其实并不难
- ThinkPHP函数详解:session方法
- cactiEZ 添加PHP扩展模块mssql及相关设置问题整理
- php json_encode() 数据
- php -- instanceof、class_exists、insterface_exists、method_exists、get_class、get_parent_class
- PHP中使用cURL实现Get和Post请求的方法