PHP input data Unified class Instance _php tutorial

Source: Internet
Author: User

PHP input data Uniform class instance


This article mainly introduces the PHP input data unified class, example analysis of the input data for various conversion skills, with a certain reference value, the need for friends can refer to the following

This article describes the PHP input data unification class. Share to everyone for your reference. Specific 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

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

Class cls_request{

private $getdata;//store data for Get

private $postdata;//Store post data

private $requestdata;//store data for request

private $filedata;//Store file data

private $cookiedata;//Storage Cooki

Static $_instance;//instances of this class

Private Function __construct () {

$this->getdata = Self::format_data ($_get);

$this->postdata = Self::format_data ($_post);

$this->requestdata = Array_merge ($this->getdata, $this->postdata);

$this->cookiedata = Self::format_data ($_cookie);

$this->filedata = Self::format_data ($_files);

}

Initializes the class, returning the Cls_request object

public static function Get_instance () {

if (! ( Self::$_instance instanceof Self)) {

Self::$_instance = new self ();

}

return self::$_instance;

}

Gets the numeric variable passed over by get

Public Function Get_num ($key) {

if (!isset ($this->getdata[$key])) {

return false;

}

return $this->to_num ($this->getdata[$key]);

}

Gets the data variable that the post passed over

Public Function Post_num ($key) {

if (!isset ($this->postdata[$key])) {

return false;

}

return $this->to_num ($this->postdata[$key]);

}

Gets the value variable passed by the request.

Public Function Request_num ($key) {

if (!isset ($this->requestdata[$key])) {

return false;

}

return $this->to_num ($this->requestdata[$key]);

}

Get the numeric variables that the cookie passes over

Public Function Cookie_num ($key) {

if (!isset ($this->cookiedata[$key])) {

return false;

}

return $this->to_num ($this->cookiedata[$key]);

}

Gets the numeric variable passed by the file.

Public Function Filedata ($key) {

return $this->filedata[$key];//returns an array

}

Gets the string variable passed over by get

Public Function get_string ($key, $isfilter =true) {

if (!isset ($this->getdata[$key])) {

return false;

}

if ($isfilter) {

return $this->filter_string ($this->getdata[$key]);

}else{

return $this->getdata[$key];

}

}

Gets the string variable that the post passed over

Public Function post_string ($key, $isfilter =true) {

if (!isset ($this->postdata[$key])) {

return false;

}

if ($isfilter) {

return $this->filter_string ($this->postdata[$key]);

}else{

return $this->postdata[$key];

}

}

Gets the string variable that the request passed over

Public Function request_string ($key, $isfilter =true) {

if (!isset ($this->requestdata[$key])) {

return false;

}

if ($isfilter) {

return $this->filter_string ($this->requestdata[$key]);

}else{

return $this->requestdata[$key];

}

}

Gets the string variable passed by the cookie.

Public Function cookie_string ($key, $isfilter =true) {

if (!isset ($this->cookiedata[$key])) {

return false;

}

if ($isfilter) {

return $this->filter_string ($this->cookiedata[$key]);

}else{

return $this->cookiedata[$key];

}

}

Formatting data

Private Function Format_data ($data) {

$result = Array ();

if (!is_array ($data)) {

$data = Array ();

}

/*

*list () indicates that the variable is assigned a value using the value of the array. Arrays that are used only for numeric indexes,

* Starting from 0 bits by default, go in order

*each ()

*/

while (list ($key, $value) = each ($data)) {//Not quite understand

Working with data such as a checkbox

if (Is_array ($value)) {

$result [$key]= $value;

}else{//General Data

$result [$key] = Trim ($value);

Remove whitespace and other predefined characters at both ends of a string

}

}

}

Conversion numbers

Private Function To_num ($num) {

if (Is_numeric ($num)) {

Return Intval ($num);//convert variable to Integer

}else{

return false;

}

}

Swap filter string

Private Function filter_string ($data) {

if ($data ===null) {

return false;

}

if (Is_array ($data)) {

foreach ($data as $k = = $v) {

$data [$k] = Htmlspecialchars ($v, ent_quotes);

Convert some pre-defined characters into HTML entities

}

return $data;

}else{//Ordinary String

Return Htmlspecialchars ($data, ent_quotes);

}

}

}

?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/966927.html www.bkjia.com true http://www.bkjia.com/PHPjc/966927.html techarticle PHP input Data Uniform class instance this article mainly introduces the PHP input data unification class, the example analyzes the various conversion techniques for the input data, has certain reference value, needs the friend ...

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