Sensitive word replacement algorithm, 4 times more efficient than str_replace (with 6,000 sensitive words attached)
Release: 2016-07-25 09:08:00
Original
1533 people have browsed it
Efficiency comparison (12688 characters, 1 replacement):
- str_replace: 0.109937906265 seconds
- strtr: 0.0306839942932 seconds
Comparison of replacement results
- For example: "Zhang San", "Zhang Sanfeng", "Zhang San Toyota" are all prohibited words (Why is there such a distinction? Please see "法X", "法Xgong")
-
Original text: "I drove a Honda Toyota to work today"
-
strtr: "I drove **** to work today" (replace all four words with *)
-
str_replace: "I drove **Toyota to work today" (only the first match was replaced)
So using str_replace to replace cannot essentially solve the problem.
Time comparison:
Number of keywords: 6712 (no duplication)
self init:0.00789093971252 (load xcache)
self:0.0354378223419
strtr:0.0311169624329
strtr_array:0.432713985443
str_replace:0.109627008438
- require('badword.src.php');
- $badword1 = array_combine($badword,array_fill(0,count($badword),'*'));
- $bb = 'I am open today San Toyota goes to work';
- $str = strtr($bb, $badword1);
Copy code
- //Interested friends can study it
- function strtr_array(&$str,&$replace_arr) {
- $maxlen = 0;$minlen = 1024*128;
- if (empty($replace_arr)) return $ str;
- foreach($replace_arr as $k => $v) {
- $len = strlen($k);
- if ($len < 1) continue;
- if ($len > $maxlen) $maxlen = $len;
- if ($len < $minlen) $minlen = $len;
- }
- $len = strlen($str);
- $pos = 0;$result = '';
- while ($pos < ; $len) {
- if ($pos + $maxlen > $len) $maxlen = $len - $pos;
- $found = false;$key = '';
- for($i = 0;$i< $maxlen;++$i) $key .= $str[$i+$pos]; //Original text: memcpy(key,str+$pos,$maxlen)
- for($i = $maxlen;$i >= $minlen;--$i) {
- $key1 = substr($key, 0, $i); //Original text: key[$i] = '
-
-
-
-
-
-
-
|
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31