Php replaces sensitive string classes (with source code)

Source: Internet
Author: User
Php replaces sensitive string classes (with source code)

  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. * Replace public replace with invalid characters
  8. * Public check whether illegal characters are contained
  9. * Private protect_white_list protection white list
  10. * Private resume_white_list restore the white list
  11. * Convert private getval White List key 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. /** Replace invalid characters
  30. * @ Param String $ content refers to the String to be replaced.
  31. * @ Return String the String after replacement
  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 whether there are invalid tokens
  50. * @ Param String $ content String
  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. /** Protect the white list
  70. * @ Param String $ content String
  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. /** Restore the 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. /** Restore the white list key to value
  92. * @ Param Array $ matches the key of white_list
  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. ?>

2. demo. php

  1. Header ("content-type: text/html; charset = utf8 ");
  2. Require ("StrFilter. class. php ");
  3. $ White = array ('Diaosi', 'caocao ');
  4. $ Black = array ('login', 'operation ');
  5. $ Content = "Fuck me, Cao, you are diaosi, I am jealous of you ";
  6. $ Obj = new StrFilter ($ white, $ black );
  7. Echo $ obj-> replace ($ content );
  8. ?>

Appendix: php replaces the class source code of 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.