Sensitive word substitution algorithm, 4 times times higher efficiency than str_replace (with 6 thousand sensitive words)

Source: Internet
Author: User

Efficiency comparison(12,688 characters, replace 1 times):
    • Str_replace: 0.109937906265 seconds
    • STRTR: 0.0306839942932 seconds

Comparison of replacement results
    • For example: "Zhang San", "Zhang San Feng", "Zhangsanfungtan" are prohibited words (why there is such a distinction?) See also "FA x", "" FA xgong")
    • " I'm driving Zhangsanfungtan to work today."
    • strtr:" I'm going to work today with * * * * (all four words replaced for *)
    • str_replace:" I'm driving to work today." (replace only the first match)
So using str_replace substitution is not an essential solution to the problem.


Time Comparison:
Number of keywords: 6712 (no Duplicates) Self init:0.00789093971252 (load XCache) self:0.0354378223419 strtr:0.0311169624329 strtr_array:0. 432713985443 str_replace:0.109627008438
    1. Require (' badword.src.php ');
    2. $badword 1 = array_combine ($badword, Array_fill (0,count ($badword), ' * '));
    3. $BB = ' I am driving Zhangsanfungtan to work today ';
    4. $str = Strtr ($BB, $badword 1);
Copy Code
  1. Interested friends can look into the
  2. Function Strtr_array (& $str,& $replace _arr) {
  3. $maxlen = 0; $minlen = 1024*128;
  4. if (Empty ($replace _arr)) return $str;
  5. foreach ($replace _arr as $k = + $v) {
  6. $len = strlen ($k);
  7. if ($len < 1) continue;
  8. if ($len > $maxlen) $maxlen = $len;
  9. if ($len < $minlen) $minlen = $len;
  10. }
  11. $len = strlen ($STR);
  12. $pos = 0; $result = ";
  13. while ($pos < $len) {
  14. if ($pos + $maxlen > $len) $maxlen = $len-$pos;
  15. $found = false; $key = ";
  16. for ($i = 0; $i < $maxlen; + + $i) $key. = $str [$i + $pos]; Original: memcpy (key,str+ $pos, $maxlen)
  17. for ($i = $maxlen; $i >= $minlen;--$i) {
  18. $key 1 = substr ($key, 0, $i); Original: key[$i] = ' + '
  19. if (Isset ($replace _arr[$key 1])) {
  20. $result. = $replace _arr[$key 1];
  21. $pos + = $i;
  22. $found = true;
  23. Break
  24. }
  25. }
  26. if (! $found) $result. = $str [$pos + +];
  27. }
  28. return $result;
  29. }
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.