Home > php教程 > PHP开发 > PHP sensitive word filtering uses the third-party extension trie_filter

PHP sensitive word filtering uses the third-party extension trie_filter

黄舟
Release: 2017-03-22 14:29:09
Original
3390 people have browsed it

Installation

ubuntu 14.04

Install libdatrie

// 
sudo apt-get install libdatrie-dev
Copy after login

Install trie_filter extension

> 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 //查看
Copy after login

Use

Generate sensitive vocabulary

 $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);
Copy after login

Use

$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);
Copy after login

The above is the content of using third-party extension trie_filter for php sensitive word filtering. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!

Related articles:

An efficient sensitive word filtering method (PHP)

Use PHP to extend trie_filter to do Chinese sensitivity Word filtering

PHP implements filtering sensitive words in message messages

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template