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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容