敏感词汇过滤 从 abc.txt 构造出 trie 树,然后匹配即可算法在精华区中 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 以 http://bbs.csdn.net/topics/390221822 的 ttrie 类为例trie = new TTrie;foreach(file('abc.txt') as $r) $trie->set(strtok($r, '|'));$s =<<< TXT输入内容坏蛋123TXT;//仅识别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输入内容坏蛋123TXT;print_r($trie->match($s));/* 得Array( [0] => 输入内容 [1] => <b>坏蛋<b/> 《== 这个就被突出显示了 [2] => 123)*/其实渔已经给你了,鱼还是自己动手的好为了使实例化的对象得以复用,还应在 match 方法开始处加入 $this->buffer = array(); $this->input = 0; $this->backtracking = 0;以重新初始化数据指针 我如何在没有key的情况下获取value? 汗,找了N多个工作,看N多个前辈的经历...怎么都是学JAVA后相当一部分学PHP了... ajax的安全问题。请教 PHP截取中文字符串中的中文 关于php与mysql_connect与mysql_pconnect 请推荐一个 LINUX/PHP/MYSQL 虚拟主机空间 php 认证问题? 请问怎样把参数变成一串乱七八糟的字符?? 如何实现图文混排? php实现简单的抽奖,有什么好方法? 求助!!php+ajax+ json 问题 想学php?
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
)
*/其实渔已经给你了,鱼还是自己动手的好为了使实例化的对象得以复用,还应在 match 方法开始处加入 $this->buffer = array();
$this->input = 0;
$this->backtracking = 0;
以重新初始化数据指针