<?php

ini_set('display_errors', 1);
$arrWord = array('word', 'word1', 'word3', 'a', 'ab', 'abc', 'b');
$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');
$resTrie = trie_filter_load(__DIR__ . '/blackword.tree');
$str = 'hello word2 haha word1 word4 word2';
$arrRet = trie_filter_search($resTrie, $str);
print_all($str, array($arrRet));
//Array(0 => 6, 1 => 5)
echo "\ntest1///////////////////\n";
$str = 'hello word2 haha word1 word4 word2';
$arrRet = trie_filter_search_all($resTrie, $str);
print_all($str, $arrRet);
echo "\ntest2///////////////////\n";
$str = 'hello word';
$arrRet = trie_filter_search($resTrie, $str);
print_all($str, array($arrRet));
//Array()
$arrRet = trie_filter_search_all($resTrie, 'hello word');
print_all($str, $arrRet);
echo "\ntest3///////////////////\n";
echo "start memory=" . memory_get_usage(true) . "\n";
date_default_timezone_set('Asia/Chongqing');
$test = array('a', 'abd', 'dad', 'pab', 'dda', 'word1f', 'cword1', 'cword1t');
foreach ($test as $v) {
<?php

ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE);
$arrWord = array('Key', 'w ord', '12345678');
$resTrie = trie_filter_new();
echo "\nsave key\n";
foreach ($arrWord as $k => $v) {
    echo "id:", $k, ' -> ', $v, "\n";
    trie_filter_store($resTrie, $k, $v, TRIE_FILTER_UP | TRIE_FILTER_SP | TRIE_FILTER_NUM);
}
//去掉关键字中的空格 小写转为大写 数字转为0
//实际相当于 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
Exemplo n.º 3
0
 /**
  * 重新生成敏感词树结构文件
  */
 public function create($words)
 {
     $trie = trie_filter_new();
     foreach ($words as $word) {
         trie_filter_store($trie, $word);
     }
     trie_filter_save($trie, $this->dirname . DIRECTORY_SEPARATOR . $this->defaultFileName);
 }