php的trie_filter扩展安装敏感词查找
#编译libiconv ./configure make make install #编译libdatrie-0.2.11 ./configure LDFLAGS=-L/usr/local/lib LIBS=-liconv --host=x86_64 make && make install #编译扩展 ./configure --with-php-config=/usr/bin/php-config make && make install #重启php-fpm service php-fpm restart
/**添加词库**/ //$data = ['我是日本人', 'sb','SB']; $resTrie = trie_filter_new(); foreach ($sensitiveWord['data'] as $k => $v) { trie_filter_store($resTrie, $v); } trie_filter_save($resTrie, $path . '/extensions/sensitive_word.tree'); trie_filter_free($resTrie); /**敏感词替换**/ $trieWord = trie_filter_load($path . '/extensions/sensitive_word.tree'); //$trieResult 返回找到的所有敏感词 //$arrRet 返回找到的1条敏感词 // //$trieResult = trie_filter_search_all($trieWord, $content); $arrRet = trie_filter_search($resTrie, $strContent); $sensitiveWord = substr($content, $arrRet[0], $arrRet[1]);//通过substr获取匹配到的文字
扩展文件:链接: https://pan.baidu.com/s/1LAiWIf4msBp-zGB0abAkkg 提取码: n9am