PHP实现非法词汇过滤的方法及代码是什么
Admin 2022-11-18 群英技术资讯 755 次浏览
这篇文章我们来了解“PHP实现非法词汇过滤的方法及代码是什么”的内容,小编通过实际的案例向大家展示了操作过程,简单易懂,有需要的朋友可以参考了解看看,那么接下来就跟随小编的思路来往下学习吧,希望对大家学习或工作能有帮助。
将关键词构造成一颗树,每个字都是一个节点。
遍历需要过滤的语句,将语句的每个字都去树中查找,看看是否存在。
构造一棵树简单,关键点是php中遍历字符串需要自己正确的得到单个字符的长度。
简单遍历字符串的方法如下:
$strLen = mb_strlen($str);
for ($i = 0; $i < $strLen; $i++) {
echo mb_substr($str, $i, 1, "utf8"),PHP_EOL;
} 登录后复制 该方法是利用mb_*系列函数来正确截取每个字符,处理大量字符串时速度非常慢,我猜测是:mb_substr每截取一个字符,都要计算该字符串之前,有多少个字符。
正确的遍历字符串的方式是按utf8的编码规律来截取字符串,具体请看下文。
<?php
/**
* 非法关键词检查
*/
class SensitiveWords
{
protected $tree = null;
protected $callIsNumeric = true;
/**
* 非法词汇列表,一个非法词汇占用一行
*/
public function __construct($path = __DIR__ . '/sensitiveWords.txt')
{
$this->tree = new WordNode();
$file = fopen($path, "r");
while (!feof($file)) {
$words = trim(fgets($file));
if ($words == '') {
continue;
}
//存在纯数字的非法词汇
if (is_numeric($words)) {
$this->callIsNumeric = false;
}
$this->setTree($words);
}
fclose($file);
}
protected function setTree($words)
{
$array = $this->strToArr($words);
$tree = $this->tree;
$l = count($array) - 1;
foreach ($array as $k => $item) {
$tree = $tree->getChildAlways($item);
if ($l == $k) {
$tree->end = true;
}
}
}
/**
* 返回包含的非法词汇
* @param string $str
* @return array
*/
public function check($str)
{
//先压缩字符串
$str = trim(str_replace([' ', "\n", "\r"], ['', '', ''], $str));
$ret = [];
loop:
$strLen = strlen($str);
if ($strLen === 0) {
return array_unique($ret);
}
//非法词汇中没有纯数字的非法词汇,待检测字符串又是纯数字的,则跳过不再检查
if ($this->callIsNumeric && is_numeric($str)) {
return array_unique($ret);
}
//挨个字符进行判断
$tree = $this->tree;
$words = '';
for ($i = 0; $i < $strLen; $i++) {
//unicode范围 --> ord 范围
//一字节 0-127 --> 0 - 127
//二字节 128-2047 --> 194 - 223
//三字节 2048-65535 --> 224 - 239
//四字节 65536-1114111 --> 240 - 244
//@see http://shouce.jb51.net/gopl-zh/ch3/ch3-05.html
$ord = ord($str[$i]);
if ($ord <= 127) {
$word = $str[$i];
} elseif ($ord <= 223) {
$word = $str[$i] . $str[$i + 1];
$i += 1;
} elseif ($ord <= 239) {
$word = $str[$i] . $str[$i + 1] . $str[$i + 2];
$i += 2;
} elseif ($ord <= 244) {
//四字节
$word = $str[$i] . $str[$i + 1] . $str[$i + 2] . $str[$i + 3];
$i += 3;
} else {
//五字节php都溢出了
//Parse error: Invalid UTF-8 codepoint escape sequence: Codepoint too large
continue;
}
//判断当前字符
$tree = $tree->getChild($word);
if (is_null($tree)) {
//当前字不存在,则截取后再次循环
$str = substr($str, $i + 1);
goto loop;
} else {
$words .= $word;
if ($tree->end) {
$ret[] = $words;
}
}
}
return array_unique($ret);
}
protected function strToArr($str)
{
$array = [];
$strLen = mb_strlen($str);
for ($i = 0; $i < $strLen; $i++) {
$array[] = mb_substr($str, $i, 1, "utf8");
}
return $array;
}
}
/**
* 单个字符的节点
*/
class WordNode
{
//是否为非法词汇末级节点
public $end = false;
//子节点
protected $child = [];
/**
* @param string $word
* @return WordNode
*/
public function getChildAlways($word)
{
if (!isset($this->child[$word])) {
$this->child[$word] = new self();
}
return $this->child[$word];
}
/**
* @param string $word
* @return WordNode|null
*/
public function getChild($word)
{
if ($word === '') {
return null;
}
if (isset($this->child[$word])) {
return $this->child[$word];
}
return null;
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
php怎样执行shell命令?这篇文章主要给大家演示一些普通的shell命令,有这方面学习需要的朋友可以看一下php执行shell命令的流程,以下有具体的代码。
今天小编就为大家分享一篇解决laravel中日志权限莫名变成了root的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
今天小编就为大家分享一篇关于PHP getDocNamespaces()函数讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
如何用php获取当前日期的7天内的时间,比如今天是5号,我想获取5号前7天的日期,怎么获取?实现方法如下:如何用php获取当前日期的7天内的时间实现方法如下:$t=time()+3600*8;//这里和标准时间相差8小时需要补足$tget=$t-3600*24*5;//比如5天前的时间echodate("Y-m-dH:i:s星期w",$tget);//格式按你需要选取例:$che
在thinkphp5中,save()方法用于向指定数据表中添加一条记录,该方法每次仅能向表中添加一条新记录,添加多条可重复执行,语法为“模型对象 -> save(数组)”或“模型对象 -> data(数组) -> save();”。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008