php 接入黑词库,php敏感词过滤使用第三方扩展trie_filter

安装

ubuntu 14.04

安装 libdatrie

//http://linux.thai.net/~thep/datrie/datrie.html#Download下不下来,只好直接装

sudo apt-get install libdatrie-dev

安装 trie_filter 扩展

> git clone https://github.com/wulijun/php-ext-trie-filter.git

> 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);

作者:yaokui

链接:https://www.jianshu.com/p/ed3fe9f5badd

來源:简书

简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值