• 技术文章 >php教程 >PHP开发

    php敏感词过滤使用第三方扩展trie_filter

    黄舟黄舟2017-03-22 14:29:09原创2104

    安装

    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中文网(m.sbmmt.com)!

    相关文章:

    一个高效的敏感词过滤方法(PHP)

    利用PHP扩展trie_filter做中文敏感词过滤

    PHP实现过滤留言信息中的敏感词

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:php,trie_filter
    上一篇:利用PHP扩展trie_filter做中文敏感词过滤 下一篇:seajs模块之间依赖的加载以及模块的执行
    千万级数据并发解决方案

    相关文章推荐

    • linux awk命令详解• Symfony2实现从数据库获取数据的方法小结• Zend Framework教程之Zend_Db_Table用法详解• PHP 页面编码声明方法详解(header或meta)• struts2的<s:iterator >标签的用法
    1/1

    PHP中文网