摘要:本文主要向大家介绍了PHP语言的替换敏感字符串,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。
本文主要向大家介绍了PHP语言的替换敏感字符串,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。
StrFilter.class.php
[php] view plain copy
1. <?php
2. /** string filter class
3. * Date: 2013-01-09
4. * Author: fdipzone
5. * Ver: v1.0
6. *
7. * Func:
8. * public replace 替换非法字符
9. * public check 检查是否含有非法字符
10. * private protect_white_list 保护白名单
11. * private resume_white_list 还原白名单
12. * private getval 白名单 key转为value
13. */
14. class StrFilter{ // class start
15.
16. private $_white_list = array();
17. private $_black_list = array();
18. private $_replacement = '*';
19. private $_LTAG = '[[##';
20. private $_RTAG = '##]]';
21.
22.
23. /**
24. * @param Array $white_list
25. * @param Array $black_list
26. * @param String $replacement
27. */
28. public function __construct($white_list=array(), $black_list=array(), $replacement='*'){
29. $this->_white_list = $white_list;
30. $this->_black_list = $black_list;
31. $this->_replacement = $replacement;
32. }
33.
34.
35. /** 替换非法字符
36. * @param String $content 要替換的字符串
37. * @return String 替換后的字符串
38. */
39. public function replace($content){
40.
41. if(!isset($content) || $content==''){
42. return '';
43. }
44.
45. // protect white list
46. $content = $this->protect_white_list($content);
47.
48. // replace black list
49. if($this->_black_list){
50. foreach($this->_black_list as $val){
51. $content = str_replace($val, $this->_replacement, $content);
52. }
53. }
54.
55. // resume white list
56. $content = $this->resume_white_list($content);
57.
58. return $content;
59. }
60.
61.
62. /** 检查是否含有非法自符
63. * @param String $content 字符串
64. * @return boolean
65. */
66. public function check($content){
67.
68. if(!isset($content) || $content==''){
69. return true;
70. }
71.
72. // protect white list
73. $content = $this->protect_white_list($content);
74.
75. // check
76. if($this->_black_list){
77. foreach($this->_black_list as $val){
78. if(strstr($content, $val)!=''){
79. return false;
80. }
81. }
82. }
83.
84. return true;
85. }
86.
87.
88. /** 保护白名单
89. * @param String $content 字符串
90. * @return String
91. */
92. private function protect_white_list($content){
93. if($this->_white_list){
94. foreach($this->_white_list as $key=>$val){
95. $content = str_replace($val, $this->_LTAG.$key.$this->_RTAG, $content);
96. }
97. }
98. return $content;
99. }
100.
101.
102. /** 还原白名单
103. * @param String $content
104. * @return String
105. */
106. private function resume_white_list($content){
107. if($this->_white_list){
108. $content = preg_replace_callback("/
\[##(.*?)##
109. \].*?/si", array($this, 'getval'), $content);
110. }
111. return $content;
112. }
113.
114.
115. /** 白名单 key还原为value
116. * @param Array $matches 匹配white_list的key
117. * @return String white_list val
118. */
119. private function getval($matches){
120. return isset($this->_white_list[$matches[1]])? $this->_white_list[$matches[1]] : ''; // key->val
121. }
122.
123. } // class end
124.
125. ?>
demo
[php] view plain copy
1. <?php
2. header("content-type:text/html;charset=utf8");
3.
4. require("StrFilter.class.php");
5.
6. $white = array('屌丝', '曹操');
7. $black = array('屌', '操');
8.
9. $content = "我操,曹操你是屌丝,我屌你啊";
10.
11. $obj = new StrFilter($white, $black);
12. echo $obj->replace($content);
13. ?>
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言PHP频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号