ucenter中词语过滤原理分析

yipeiwu_com5年前PHP代码库

本文分析了ucenter中词语过滤原理。分享给大家供大家参考,具体如下:

过滤词语表:

id admin find replacement findpattern
1 UCenterAdminist 访问 /访问/is
2 UCenterAdminist 4655 45 /4655/is
3 UCenterAdminist fdsaf dfsa /fdsaf/is
4 UCenterAdminist 有机会 /有机会/is

组建缓存数据:

//private
function _get_badwords() {
  $data = $this->db->fetch_all("SELECT * FROM ".UC_DBTABLEPRE."badwords");
  $return = array();
  if(is_array($data)) {
    foreach($data as $k => $v) {
      $return['findpattern'][$k] = $v['findpattern'];
      $return['replace'][$k] = $v['replacement'];
    }
  }
  return $return;
}

调用方法:

$_CACHE['badwords'] = $this->base->cache('badwords');
if($_CACHE['badwords']['findpattern']) {
  $subject = @preg_replace($_CACHE['badwords']['findpattern'], $_CACHE['badwords']['replace'], $subject);
  $message = @preg_replace($_CACHE['badwords']['findpattern'], $_CACHE['badwords']['replace'], $message);
}

preg_replace() 的每个参数(除了 limit)都可以是一个数组。如果 pattern 和 replacement 都是数组,将以其键名在数组中出现的顺序来进行处理。这不一定和索引的数字顺序相同。如果使用索引来标识哪个 pattern 将被哪个 replacement 来替换,应该在调用 preg_replace() 之前用 ksort() 对数组进行排序。

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php安全过滤技巧总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

Laravel中使用阿里云OSS Composer包分享

阿里云提供了基于命名空间的 V2 版 SDK,但是文档不是很完整,使用门槛比较高,于是我封装了一个 Composer 包:https://github.com/johnlui/Aliyu...

php日期转时间戳,指定日期转换成时间戳

写过PHP+MySQL的程序员都知道有时间差,UNIX时间戳和格式化日期是我们常打交道的两个时间表示形式,Unix时间戳存储、处理方便,但是不直观,格式化日期直观,但是处理起来不如Uni...

网页上facebook分享功能具体实现

复制代码 代码如下: <span style=" font-family: Verdana, Arial, Helvetica, sans-serif; word-wrap: no...

php中OR与|| AND与&amp;&amp;的区别总结

php中OR与|| AND与&amp;&amp;的区别总结

本身没有区别,习惯问题 ,但是有时候牵涉到运算符优先级的问题,结果会不同,记录下。 例如: 复制代码 代码如下:$p = 6 or 0; var_dump($p);//int(6) $p...

PHP反射原理与用法深入分析

本文实例讲述了PHP反射原理与用法。分享给大家供大家参考,具体如下: 说到反射,实际上包含两个概念: 检视 introspection 判断类、方法是否存在,父子类关系,调用关系等...