PHP replaces sensitive string classes (with source code)

Source: Internet
Author: User
  1. /** String Filter Class
  2. * date:2013-01-09
  3. * Author:fdipzone
  4. * ver:v1.0
  5. * edit:bbs.it-home.org
  6. * Func:
  7. * Public replace replaces illegal characters
  8. * Public check Check to see if illegal characters are included
  9. * Private protect_white_list protection Whitelist
  10. * Private resume_white_list Restore white list
  11. * Private getval Whitelist key converted to value
  12. */
  13. Class strfilter{//Class start
  14. Private $_white_list = Array ();
  15. Private $_black_list = Array ();
  16. Private $_replacement = ' * ';
  17. Private $_ltag = ' [[# # ';
  18. Private $_rtag = ' # #]] ';
  19. /**
  20. * @param Array $white _list
  21. * @param Array $black _list
  22. * @param String $replacement
  23. */
  24. Public function __construct ($white _list=array (), $black _list=array (), $replacement = ' * ') {
  25. $this->_white_list = $white _list;
  26. $this->_black_list = $black _list;
  27. $this->_replacement = $replacement;
  28. }
  29. /** replacing illegal characters
  30. * @param string $content to swap
  31. * string replaced by @return strings
  32. */
  33. Public function Replace ($content) {
  34. if (!isset ($content) | | $content = = ") {
  35. Return ';
  36. }
  37. Protect White List
  38. $content = $this->protect_white_list ($content);
  39. Replace Black List
  40. if ($this->_black_list) {
  41. foreach ($this->_black_list as $val) {
  42. $content = Str_replace ($val, $this->_replacement, $content);
  43. }
  44. }
  45. Resume White List
  46. $content = $this->resume_white_list ($content);
  47. return $content;
  48. }
  49. /** check for illegal self-characters
  50. * @param string $content strings
  51. * @return Boolean
  52. */
  53. Public function Check ($content) {
  54. if (!isset ($content) | | $content = = ") {
  55. return true;
  56. }
  57. Protect White List
  58. $content = $this->protect_white_list ($content);
  59. Check
  60. if ($this->_black_list) {
  61. foreach ($this->_black_list as $val) {
  62. if (Strstr ($content, $val)! = ") {
  63. return false;
  64. }
  65. }
  66. }
  67. return true;
  68. }
  69. White List of/** protection
  70. * @param string $content strings
  71. * @return String
  72. */
  73. Private Function Protect_white_list ($content) {
  74. if ($this->_white_list) {
  75. foreach ($this->_white_list as $key = + $val) {
  76. $content = Str_replace ($val, $this->_ltag. $key. $this->_rtag, $content);
  77. }
  78. }
  79. return $content;
  80. }
  81. /** to restore white list
  82. * @param String $content
  83. * @return String
  84. */
  85. Private Function Resume_white_list ($content) {
  86. if ($this->_white_list) {
  87. $content = Preg_replace_callback ("/\[\[## (. *?) ##\]\].*?/si ", Array ($this, ' getval '), $content);
  88. }
  89. return $content;
  90. }
  91. /** White list key reverts to value
  92. * @param Array $matches match white_list Key
  93. * @return String white_list val
  94. */
  95. Private Function Getval ($matches) {
  96. return Isset ($this->_white_list[$matches [1]])? $this->_white_list[$matches [1]]: '; Key->val
  97. }
  98. }//Class end
  99. ?>
Copy Code

2, demo example demo.php

    1. Header ("Content-type:text/html;charset=utf8");
    2. Require ("StrFilter.class.php");
    3. $white = array (' Cock silk ', ' Caocao ');
    4. $black = array (' Cock ', ' fuck ');
    5. $content = "I fuck, you are a dick, I dick You";
    6. $obj = new Strfilter ($white, $black);
    7. echo $obj->replace ($content);
    8. ?>
Copy Code

Attached, PHP replaces the class source download address for sensitive strings.

  • 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.