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] = '
-
-
-
-
-
-
-
|