PHP implements a replacement sensitive string class instance _php tutorial

Source: Internet
Author: User
Tags php class

PHP implements an instance of replacing a sensitive string class


The StrFilter.class.php class file is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

/** String Filter Class

* date:2013-01-09

* Author:fdipzone

* ver:v1.0

*

* Func:

* Public replace replaces illegal characters

* Public check Check to see if illegal characters are included

* Private protect_white_list protection Whitelist

* Private resume_white_list Restore white list

* Private getval Whitelist key converted to value

*/

Class strfilter{//Class start

Private $_white_list = Array ();

Private $_black_list = Array ();

Private $_replacement = ' * ';

Private $_ltag = ' [[# # ';

Private $_rtag = ' # #]] ';

/**

* @param Array $white _list

* @param Array $black _list

* @param String $replacement

*/

Public function __construct ($white _list=array (), $black _list=array (), $replacement = ' * ') {

$this->_white_list = $white _list;

$this->_black_list = $black _list;

$this->_replacement = $replacement;

}

/** replacing illegal characters

* @param string $content to swap

* string replaced by @return strings

*/

Public function Replace ($content) {

if (!isset ($content) | | $content = = ") {

Return ';

}

Protect White List

$content = $this->protect_white_list ($content);

Replace Black List

if ($this->_black_list) {

foreach ($this->_black_list as $val) {

$content = Str_replace ($val, $this->_replacement, $content);

}

}

Resume White List

$content = $this->resume_white_list ($content);

return $content;

}

/** check for illegal self-characters

* @param string $content strings

* @return Boolean

*/

Public function Check ($content) {

if (!isset ($content) | | $content = = ") {

return true;

}

Protect White List

$content = $this->protect_white_list ($content);

Check

if ($this->_black_list) {

foreach ($this->_black_list as $val) {

if (Strstr ($content, $val)! = ") {

return false;

}

}

}

return true;

}

White List of/** protection

* @param string $content strings

* @return String

*/

Private Function Protect_white_list ($content) {

if ($this->_white_list) {

foreach ($this->_white_list as $key = + $val) {

$content = Str_replace ($val, $this->_ltag. $key. $this->_rtag, $content);

}

}

return $content;

}

/** to restore white list

* @param String $content

* @return String

*/

Private Function Resume_white_list ($content) {

if ($this->_white_list) {

$content = Preg_replace_callback ("/\[\[## (. *?) ##\]\].*?/si ", Array ($this, ' getval '), $content);

}

return $content;

}

/** White list key reverts to value

* @param Array $matches match white_list Key

* @return String white_list val

*/

Private Function Getval ($matches) {

return Isset ($this->_white_list[$matches [1]])? $this->_white_list[$matches [1]]: '; Key->val

}

}//Class end

?>

The demo sample is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

Header ("Content-type:text/html;charset=utf8");

Require ("StrFilter.class.php");

$white = array (' Cock silk ', ' Caocao ');

$black = array (' Cock ', ' fuck ');

$content = "I fuck, you are a dick, I dick You";

$obj = new Strfilter ($white, $black);

echo $obj->replace ($content);

?>

http://www.bkjia.com/PHPjc/882702.html www.bkjia.com true http://www.bkjia.com/PHPjc/882702.html techarticle PHP implements a replacement sensitive string class instance of the StrFilter.class.php class file as follows: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 It's a ...

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