Пример #1
0
function clear_word($contents)
{
    // 载入词典,成功返回一个 Trie_Filter 资源句柄,失败返回 NULL
    return $contents;
    if ($contents == '') {
        return '';
    }
    $file = trie_filter_load('./word-filter.dic');
    // 检测文本中是否含有词典中定义的敏感词(假设敏感词设定为:‘敏感词’)
    $res = trie_filter_search($file, $contents);
    var_dump($res);
    if (empty($res) == false) {
        trie_filter_free($file);
        //最后别忘记调用free
        return '';
    }
    //$res1 = trie_filter_search_all($file, $str1);  // 一次把所有的敏感词都检测出来
    //$res2 = trie_filter_search($file, $str2);// 每次只检测一个敏感词
    trie_filter_free($file);
    //最后别忘记调用free
    return $contents;
}
//实际相当于 array('WORD', 'KEY', '00000000');
trie_filter_save($resTrie, __DIR__ . '/blackword.tree');
$resTrie = trie_filter_load(__DIR__ . '/blackword.tree');
$strContent = 'hello wo rd WORD kEy 0123456789';
$arrRet = trie_filter_search_all($resTrie, $strContent, TRIE_FILTER_UP | TRIE_FILTER_SP | TRIE_FILTER_NUM);
//小写转为大写 数字转为0 忽略文本中的空格
//实际相当于 'HELLOWORDWORDKEY0000000000'
print_all($strContent, $arrRet);
function print_all($str, $res)
{
    echo "\ntext:{$str}\n", "\nmatch ", count($res) / 3, "\n";
    for ($i = 0, $c = count($res); $i < $c; $i += 3) {
        echo 'id:', $res[$i + 2], ' -> ', substr($str, $res[$i], $res[$i + 1]), "\n";
    }
}
trie_filter_free($resTrie);
/*输出为

save key
id:0 -> Key
id:1 -> w ord
id:2 -> 12345678

text:hello wo rd WORD kEy 0123456789

match 6
id:1 -> wo rd
id:1 -> WORD
id:0 -> kEy
id:2 -> 01234567
id:2 -> 12345678
Пример #3
0
 public function __destruct()
 {
     if (is_resource($this->trie)) {
         trie_filter_free($this->trie);
     }
 }