当前位置: 移动技术网 > IT编程>开发语言>PHP > ucenter中词语过滤原理分析

ucenter中词语过滤原理分析

2017年12月12日  | 移动技术网IT编程  | 我要评论

鼠标dpi,proe4.0免费下载,李开复的创新工场

本文分析了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程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网