安装
ubuntu 14.04
安装 libdatrie
//
sudo apt-get install libdatrie-dev
安装 trie_filter 扩展
> git clone
> cd php-ext-trie-filter
> phpize
> ./configure --with-php-config=/usr/bin/php-config
> make
> make install//添加php扩展,extension=trie_filter.so
> service php5-fpm restart
> php5-fpm -m //查看
使用
生成敏感词库
$arrword = array('word1', 'word2', 'word3');
$restrie = trie_filter_new(); //create an empty trie tree
foreach ($arrword as $k => $v) {
trie_filter_store($restrie, $v);
}
trie_filter_save($restrie, __dir__ . '/blackword.tree');
trie_filter_free($restrie);
使用
$restrie = trie_filter_load(__dir__ . '/blackword.tree');
$strcontent = 'hello word2 word1';
$arrret = trie_filter_search($restrie, $strcontent);
print_r($arrret); //array(0 => 6, 1 => 5)
echo substr($strcontent, $arrret[0], $arrret[1]); //word2
$arrret = trie_filter_search_all($restrie, $strcontent);
print_r($arrret); //array(0 => array(0 => 6, 1 => 5), 1 => array(0 => 12, 1 => 5))
foreach ($arrret as $item) { echo substr($strcontent, $item[0], $item[1]); //word2 word1
} $arrret = trie_filter_search($restrie, 'hello word');
print_r($arrret); //array()
trie_filter_free($restrie);
以上就是php敏感词过滤使用第三方扩展trie_filter的内容。
相关文章:
一个高效的敏感词过滤方法(php)
利用php扩展trie_filter做中文敏感词过滤
php实现过滤留言信息中的敏感词