php纯净版的脏字过滤算法,此版本是替换全文最后出现的关键字
参考地址:http://www.cnblogs.com/xingd/archive/2008/01/23/1050443.html
稍加修改的,替换全最后出现的关键字
<?php set_time_limit(0); error_reporting(E_ERROR | E_WARNING | E_PARSE); header("Content-type: text/html; charset=utf-8"); class efilter { private $keys, $str, $cKeys, $firstKey, $dkeys, $mLen = 0; /** *@str 需要替换的文本 *@keys 替换的关键字数组 例如 array("key" => "repalceStr"); **/ public function __construct($str, array $keys) { $this->keys = $keys; $this->str = $str; $this->firstKey = array(); $this->dkeys = array(); $this->checkKeys(); } public function filter() { $keys = $this->keys; $fkey = $this->firstKey; $dkey = $this->dkeys; $str = &$this->str; // var_dump($fkey); $tmpArray = array(); $keythis = array(); // var_dump($keys[substr($str, 27, 3)]); for ($i = 0, $L = strlen($str); $i < $L; $i++) { $s = ""; $tmp = ""; //echo $fkey[$str{$i}]. ":test.i:{$str{$i}}"; while ($i < $L && !$fkey[$str{$i}]) { $s .= $str{$i++}; } $ts = $i; //echo "{$s}:--{$i}<br>"; for ($j = 0, $len = min($this->mLen, $L - $i); $j < $len; $j++) { //echo "...."; $stmp = $str{$j + $i}; $tmp .= $stmp; if (!isset($dkey[$stmp])) { //echo "找出的........<br>"; $i = $i + $j; $s .= $tmp; $tmp = ""; break; } elseif (isset($keys[$tmp])) { $i = $i + $j; // echo $tmp.":关键子<BR>"; break; } } //echo "<br>c:{$i}<br>"; if ($ts == $i) { $s .= $str{$i}; } !empty($s) && array_push($tmpArray, $s); if (!empty($tmp)) { $c = count($tmpArray); array_push($tmpArray, $tmp); isset($keythis[$tmp]) && ($keythis[$tmp] = $tmp); $keythis[$tmp] = &$tmpArray[$c]; $keythis[$tmp] = $keys[$tmp]; } } return implode("", $tmpArray); } private function checkKeys() { $keys = &$this->keys; $fkey = &$this->firstKey; $dkey = &$this->dkeys; foreach ($keys as $key => $v) { // echo "{$key}<Br>"; $fkey[$key{0}] = true; for ($i = 0, $L = strlen($key); $i < $L; $i++) { $dkey[$key{$i}] = true; } $this->mLen = max($this->mLen, strlen($key)); } //echo $this->mLen; //echo $dkey[$this->str{0}]; //var_dump($dkey); } } class Timer { //声明一个计算脚本运行时间的类 private $startTime; //保存脚本开始执行时的时间(以微秒的形式保存) private $stopTime; //保存脚本结束执行时的时间(以微秒的形式保存) function __construct() { //构造方法,在创建对象时初始化成员属性 $this->startTime = 0; //初始化成员属性startTime的值为0 $this->stopTime = 0; //初始化成员属性stopTime的值为0 } function start() { //在脚本开始处调用获取脚本开始时间的微秒值 $this->startTime = microtime(true); //将获取的时间赋给成员属性$startTime } function stop() { //在脚本结束处调 用获取脚本结束时间的微秒值 $this->stopTime = microtime(true); //将获取的时间赋给成员属性$stopTime } function spent() { //返回同一脚本中两次获取时间的差值 return round(($this->stopTime - $this->startTime), 4); //计算后以4舍5入保留4位返回 } } $str = "美食天下,有很好吃的ckkd美食。,也是全城最美的美食城,也是全城最美的美食城,也是全城最美的美食城,也是全城最美的美食城,也是全城美食天下,有很好吃的ckkd美食。,也是全城最美的美食城,也是全城最美的美食城,也是全城最美的美食城,也是全城最美的美食城,也是全城美食天下,有很好吃的ckkd美食。,也是全城最美的美食城,也是全城最美的美食城,也是全城最美的美食城,也是全城最美的美食城,也是全城美食天下,有很好吃的ckkd美食。,也是全城最美的美食城,也是全城最美的美食城,也是全城最美的美食城,也是全城最美的美食城,也是全城美食天下,有很好吃的ckkd美食。,也是全城最美的美食城,也是全城最美的美食城,也是全城最美的美食城,也是全城最美的美食城,也是全城美食天下,有很好吃的ckkd美食。,也是全城最美的美食城,也是全城最美的美食城,也是全城最美的美食城,也是全城最美的美食城,也是全城美食天下,有很好吃的ckkd美食。,也是全城最美的美食城,也是全城最美的美食城,也是全城最美的美食城,也是全城最美的美食城,也是全城美食天下,有很好吃的ckkd美食。,也是全城最美的美食城,也是全城最美的美食城,也是全城最美的美食城,也是全城最美的美食城,也是全城最美的美食城"; $str .= $str; echo "{$str}<br>"; $key = array( "的" => "[替换的]", "美食" => "[替换美食]", "城" => "[替换城]", "是" => "[替换是]" ); $t = new Timer(); $k = new efilter($str, $key); $t->start(); echo $k->filter(); $t->stop(); echo microtime(true); echo "tiem:" . time(); echo "<br>脚本替换时间为:" . $t->spent() . " 秒 字符串大小:" .round(strlen($str)/1024, 3). "KB, 关键字数量为:". count($key)."个"; ?>