php libdatrie win,centos下php扩展trie_filter过滤关键词

在项目中需要对用户传递过来的文字进行过滤敏感词,关键词过滤扩展,用于检查一段文本中是否出现敏感词,基于Double-Array Trie 树实现

安装 libdatrie , 需要 libdatrie-0.2.4 或更新的版本

依赖 libiconv .安装

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz

tar -zxf libiconv-1.14.tar.gz

cd libiconv-1.14

./configure

make && make install

libdatrie 安装:

wget ftp://linux.thai.net/pub/ThaiLinux/software/libthai/libdatrie-0.2.4.tar.gz

tar -zxf libdatrie-0.2.4.tar.gz

cd libdatrie-0.2.4

./configure --prefix=/usr/local/libdatrie/

make ICONV_LIBS='/usr/local/lib/libiconv.so'

make install

安装 PHP 扩展

wget https://github.com/wulijun/php-ext-trie-filter/archive/master.zip

unzp master.zip

cd php-ext-trie-filter-master/

phpize

./configure --with-php-config=/usr/local/php/bin/php-config --with-trie_filter=/usr/local/libdatrie/

make && make install

将生成的 trie_filter.so 文件复制到 php 扩展目录,并在 php.ini 中添加该文件

安装完后,在 phpinfo 里可以看到 trie_filter 扩展,或者输入php -m查看

使用示例

echo "hello,www.2kb.com"."
";

$arrWord = array('word1', 'word2', 'test','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');

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

$arrRet = trie_filter_search($resTrie, 'hello word');

print_r($arrRet); //Array()

trie_filter_free($resTrie);

?>

2KB项目(www.2kb.com,软件服务网),提供担保交易、源码交易、虚拟商品、任务交易、网站设计、软件设计、站长交易、域名交易、链接交换、网站交易、广告交易、站长培训、建站美工等服务

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值