敏感词汇过滤
假如用户输入了
$str="输入内容123";
坏蛋|1
火枪|1
超级大坏蛋|1
....
trie = new TTrie;
foreach(file('abc.txt') as $r) $trie->set(strtok($r, '
[解决办法]
'));
$s =<<< TXT
输入内容坏蛋123
TXT;
//仅识别
print_r($trie->match($s));
/* 得
Array
(
[0] => 输入内容
[1] => 坏蛋
[2] => 123
)
*/
//去掉字典字
$trie->savematch = 0;
print_r($trie->match($s));
/* 得
Array
(
[0] => 输入内容
[1] => 123
)
*/
class mytrie extends ttrie {
function b() {
$this->buffer[] = '<b>' . array_pop($this->buffer) . '<b/>';
}
}
$trie = new mytrie;
foreach(file('abc.txt') as $r) $trie->set(strtok($r, '
[解决办法]
'), 'b');
$s =<<< TXT
输入内容坏蛋123
TXT;
print_r($trie->match($s));
/* 得
Array
(
[0] => 输入内容
[1] => <b>坏蛋<b/> 《== 这个就被突出显示了
[2] => 123
)
*/
$this->buffer = array();以重新初始化数据指针
$this->input = 0;
$this->backtracking = 0;