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

        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號
        主站蜘蛛池模板: 久久久久久亚洲精品不卡| 无码精品人妻一区二区三区免费看| 人妻偷人精品成人AV| 国产成人精品免费视频动漫| 亚洲国产精品成人AV无码久久综合影院| 四虎精品8848ys一区二区| 国产成人亚洲精品青草天美| 亚洲国产精品一区二区久久hs| 国产亚洲精品成人a v小说| 精品一区二区在线观看| 久久精品中文无码资源站| 亚洲AV永久无码精品一区二区国产 | 99精品视频在线观看| 色欲久久久天天天综合网精品| 日本免费精品一区二区三区 | 国产精品无码国模私拍视频| 久久国产精品一区二区| 国产精品精品自在线拍| 久久夜色精品国产噜噜噜亚洲AV | 久久久久久亚洲精品不卡| 国产精品无码素人福利| 青青草国产精品| 欧美精品/日韩精品/国产精品| 97久久久久人妻精品专区| 精品久久久久久久久午夜福利 | 伊人久久精品无码av一区| 香港三级精品三级在线专区 | 国产精品大白天新婚身材| 国内精品伊人久久久久AV影院| 久久夜色精品国产欧美乱| 无码人妻丰满熟妇精品区| 亚洲精品乱码久久久久久久久久久久 | 久久久久无码国产精品不卡| 国产精品自产拍高潮在线观看| 国产精品福利电影一区二区三区四区欧美白嫩精品 | 亚洲精品乱码久久久久久按摩| 日批日出水久久亚洲精品tv| 亚洲电影日韩精品| 亚洲精品国产首次亮相| 无码精品久久久天天影视| 青青草原精品99久久精品66 |