Home  >  Article  >  php教程  >  Analysis of word filtering principles in ucenter

Analysis of word filtering principles in ucenter

高洛峰
高洛峰Original
2016-12-30 16:52:041088browse

This article analyzes the word filtering principle in ucenter. Share it with everyone for your reference, the details are as follows:

Filter word list

Analysis of word filtering principles in ucenter

Build cache data:

//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;
}

Calling method:

$_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);
}

Each parameter of preg_replace() (except limit) can be an array. If both pattern and replacement are arrays, their keys will be processed in the order in which they appear in the array. This is not necessarily the same as the numerical order of the index. If an index is used to identify which pattern is to be replaced by which replacement, the array should be sorted with ksort() before calling preg_replace().

I hope this article will be helpful to everyone in PHP programming.

For more articles related to the analysis of word filtering principles in ucenter, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn