This article analyzes the word filtering principle in ucenter. Share it with everyone for your reference, the details are as follows:
Filter word list
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!