Php-implemented replacement of sensitive string-type instances

Source: Internet
Author: User

Php-implemented replacement of sensitive string-type instances

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

21

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

<? Php

/** String filter class

* Date: 2013-01-09

* Author: fdipzone

* Ver: v1.0

*

* Func:

* Replace public replace with invalid characters

* Public check whether illegal characters are contained

* Private protect_white_list protection White List

* Private resume_white_list restore the White List

* Convert private getval white list key 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;

}

/** Replace invalid characters

* @ Param String $ content refers to the String to be replaced.

* @ Return String the String after replacement

*/

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 whether there are invalid tokens

* @ Param String $ content String

* @ 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;

}

/** Protect the White List

* @ Param String $ content String

* @ 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;

}

/** Restore the 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;

}

/** Restore the white list key to value

* @ Param Array $ matches the key of white_list

* @ 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

?>

Demo example:

1

2

3

4

5

6

7

8

9

10

11

12

13

<? Php

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

Require ("StrFilter. class. php ");

$ White = array ('diaosi', 'caocao ');

$ Black = array ('login', 'operation ');

$ Content = "Fuck me, Cao, you are diaosi, I am jealous of you ";

$ Obj = new StrFilter ($ white, $ black );

Echo $ obj-> replace ($ content );

?>

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.