站長資訊網
        最全最豐富的資訊網站

        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;     } }
        登錄后復制

        推薦學習:《PHP視頻教程》

        php入門到就業線上直播課:立即學習
        全程直播 + 實戰授課 + 邊學 + 邊練 + 邊輔導

        贊(0)
        分享到: 更多 (0)
        網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
        主站蜘蛛池模板: 欲帝精品福利视频导航| 国产精品无码a∨精品| 久久亚洲精品无码AV红樱桃| 国产精品成人国产乱一区| 国产精品亚洲а∨无码播放| 亚洲国产主播精品极品网红| 成人免费精品网站在线观看影片 | 精品久久久久久中文字幕人妻最新| 国产亚洲精品高清在线| 欧美精品在线一区| 99国产精品无码| 久久99国产综合精品女同| 亚洲精品高清一二区久久| 九九精品在线视频| 国产小呦泬泬99精品| 99久久精品国产一区二区蜜芽| 91精品一区国产高清在线| 亚洲AV永久无码精品| 亚洲AV无码成人精品区狼人影院| 国产精品综合专区中文字幕免费播放| 麻豆精品不卡国产免费看| 国产成人精品天堂| 成人区人妻精品一区二区不卡网站| 日本伊人精品一区二区三区| 亚洲а∨天堂久久精品| 欧美精品在线视频| 久久精品国产精品亚洲艾草网美妙| 国产精品亚洲综合一区| 国产精品gz久久久| 国产精品久久久久久久午夜片| 99久久99久久精品国产片| 四虎国产精品永久地址99| 久久亚洲国产欧洲精品一| 久久国产精品久久精品国产| 国产一成人精品福利网站| 欧美国产日本精品一区二区三区| 在线精品视频播放| 精品国产一区二区三区2021| 久久久久一级精品亚洲国产成人综合AV区| 精品国产日韩亚洲一区| 久久精品三级视频|