php 實現敏感詞過濾 - PHP擴充套件trie_filter
阿新 • • 發佈:2018-12-12
實現方法1 使用PHP擴充套件trie_filter
安裝:libiconv
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar zxvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure
make
make install
phpinfo() 檢視是否安裝
安裝:libdatrie
下載地址:https://linux.thai.net/~thep/datrie/datrie.html#Download 版本0.2.4
phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-trie_filter=/usr/local/lib
make
make install
網上大部分的下載包對PHP7支援有問題,從網上找了一份支援7的下載包
wget https://github.com/jiopuud/trie_filter/archive/master.zip
phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-trie_filter=/usr/local/lib
make clean
make
make install
更新php.ini檔案
增加:extension=trie_filter.so
儲存並重新啟動php-fpm服務。
phpinfo() 檢視服務安裝
/*程式碼實現敏感詞過濾*/ //生成敏感詞字典 $str = "老王,你就是個混蛋,打電話不接,發微信不回,你要是再不回信,你就是成了老混蛋了"; Filter($str); public function createDict() { $resTrie = trie_filter_new(); $words = array('混蛋','回信','發微信'); foreach ($words as $k => $v) { trie_filter_store($resTrie, $v); } trie_filter_save($resTrie,'/PATH/dict.tree'); } public function Filter($str) { $resTrie = trie_filter_new(); //載入字典 $dict = trie_filter_load('/PATH/dict.tree'); $res = trie_filter_search_all($dict, $str); var_dump($res); }
問題點總結:1 安裝libdatrie服務時,使用最新版本會報錯,最全決定使0.2.4版本
2 git上trie_filter拓展包對當前php7不支援,安裝是會報錯,所以從網上查詢一份支援php7的包。