ホームページ  >  記事  >  バックエンド開発  >  PHP_PHP チュートリアルで実装された機密文字列クラスを置き換える例

PHP_PHP チュートリアルで実装された機密文字列クラスを置き換える例

WBOY
WBOYオリジナル
2016-07-13 10:18:47755ブラウズ

phpによって実装された機密文字列クラスを置換する例

StrFilter.class.phpクラスファイルは次のとおりです:

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

/**文字列フィルタークラス

* 日付: 2013-01-09

* 著者: fdipzone

* バージョン: v1.0

*

* 機能:

* public replace 不正な文字を置換します

* public check 不正な文字が含まれているかどうかをチェックします

* private protected_white_list 保護ホワイトリスト

* プライベートresume_white_list ホワイトリストを復元します

* private getval ホワイトリストのキーを値に変換します

*/

class StrFilter{ // クラスの開始

プライベート $_white_list = array();

プライベート $_black_list = array();

プライベート $_replacement = '*';

プライベート $_LTAG = '[[##';

プライベート $_RTAG = '##]]';

/** 

* @param 配列 $white_list

* @param 配列 $black_list

* @param String $replacement

  */

パブリック関数 __construct($white_list=array(), $black_list=array(), $replacement='*'){

$this->_white_list = $white_list

$this->_black_list = $black_list

$this->_replacement = $replacement

}

/** 不正な文字を置換します

* @param String $content 置換される文字列

* @return String 代替後の文字列

*/

パブリック関数 replace($content){

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

「」を返します。 

}

// ホワイトリストを保護する

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

// ブラックリストを置き換える

if($this->_black_list){

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

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

}

}

// ホワイトリストを再開する

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

$content を返します。 

}

/**不正な自己文字が含まれていないか確認してください

* @param 文字列 $content 文字列

* @return ブール値

*/

パブリック関数チェック($content){

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

true を返します。 

}

// ホワイトリストを保護する

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

// チェックしてください

if($this->_black_list){

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

if(strstr($content, $val)!=''){

false を返します。 

}

}

}

true を返します。 

}

/**保護ホワイトリスト

* @param 文字列 $content 文字列

* @return 文字列

*/

プライベート関数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); 

}

}

$content を返します。 

}

/**ホワイトリストを復元する

* @param String $content

* @return 文字列

*/

プライベート関数resume_white_list($content){

if($this->_white_list){

$content = preg_replace_callback("/[[##(.*?)##]].*?/si", array($this, 'getval'), $content); 

}

$content を返します。 

}

/** 白名单 key还原はvalue

* @param 配列 $matches 適合white_list のキー

* @return String White_list val

*/

プライベート関数 getval($matches){

return isset($this->_white_list[$matches[1]])? $this->_white_list[$matches[1]] : ''; // キー->ヴァル

}

} // 授業終了

?>

デモの例:

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('屌丝', '曹操'); 

$black = array('屌', '操'); 

$content = "我操,曹操你是屌丝,我屌你啊"; 

$obj = 新しい StrFilter($white, $black); 

echo $obj->replace($content); 

?>

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/882702.html技術記事 php の代替敏感文字符串类例 StrFilter.class.php 文例以下: 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...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。