首頁  >  文章  >  php教程  >  php敏感詞過濾使用第三方擴充trie_filter

php敏感詞過濾使用第三方擴充trie_filter

黄舟
黄舟原創
2017-03-22 14:29:093324瀏覽

安裝

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中文網(www.php.cn)!

相關文章:

一個高效的敏感詞過濾方法(PHP)

利用PHP擴充trie_filter做中文敏感詞過濾

用PHP擴充trie_filter做中文敏感詞過濾PHP

🎜
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn