Home >Backend Development >PHP Tutorial >Usage examples of common PHP form validation classes_PHP tutorial

Usage examples of common PHP form validation classes_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:50:11844browse

Usage examples of common PHP form validation classes

This article describes the usage of common PHP form validation classes. Share it with everyone for your reference. The details are 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

116

117

118

119

120

121

122

123

124

125

126

127

128

129

/**

* Page function: Common form validation classes

* Author: Xinran Suifeng

* QQ: 276624915

*/

class class_post

{

//Verify whether it is a letter/number combination of the specified length

function fun_text1($num1,$num2,$str)

{

Return (preg_match("/^[a-zA-Z0-9]{".$num1.",".$num2."}$/",$str))?true:false;

}

//Verify whether it is a number of the specified length

function fun_text2($num1,$num2,$str)

{

return (preg_match("/^[0-9]{".$num1.",".$num2."}$/i",$str))?true:false;

}

//Verify whether it is a Chinese character of specified length

function fun_font($num1,$num2,$str)

{

// preg_match("/^[xa0-xff]{1,4}$/", $string);

return (preg_match("/^([x81-xfe][x40-xfe]){".$num1.",".$num2."}$/",$str))?true:false;

}

//Verify ID number

function fun_status($str)

{

return (preg_match('/(^([d]{15}|[d]{18}|[d]{17}x)$)/',$str))?true:false;

}

//Verify email address

function fun_email($str){

return (preg_match('/^[_.0-9a-z-] @([0-9a-z][0-9a-z-] .) [a-z]{2,4}$/' ,$str))?true:false;

}

//Verify phone number

function fun_phone($str)

{

return (preg_match("/^(((d{3}))|(d{3}-))?((0d{2,3})|0d{2,3}-)?[1 -9]d{6,7}$/",$str))?true:false;

}

//Verify zip code

function fun_zip($str)

{

return (preg_match("/^[1-9]d{5}$/",$str))?true:false;

}

//Verify url address

function fun_url($str)

{

return (preg_match("/^http://[A-Za-z0-9] .[A-Za-z0-9] [/=?%-&_~`@[]': !]* ([^<>""])*$/",$str))?true:false;

}

//Data storage escapes special characters. The incoming value can be a string or a one-dimensional array

function data_join(&$data)

{

if(get_magic_quotes_gpc() == false)

{

if (is_array($data))

{

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

{

$data[$k] = addslashes($v);

}

}

else

{

$data = addslashes($data);

}

}

Return $data;

}

//Data export and restore special characters. The input value can be a string or a one/two-dimensional array

function data_revert(&$data)

{

if (is_array($data))

{

foreach ($data as $k1 => $v1)

{

if (is_array($v1))

{

foreach ($v1 as $k2 => $v2)

{

$data[$k1][$k2] = stripslashes($v2);

}

}

else

{

$data[$k1] = stripslashes($v1);

}

}

}

else

{

$data = stripslashes($data);

}

Return $data;

}

//Data display and restore data format is mainly used for content output. The incoming value can be a string or a one/two-dimensional array

// Data_revert() should be performed before executing this method. The form content does not need to be restored

function data_show(&$data)

{

if (is_array($data))

{

foreach ($data as $k1 => $v1)

{

if (is_array($v1))

{

foreach ($v1 as $k2 => $v2)

{

$data[$k1][$k2]=nl2br(htmlspecialchars($data[$k1][$k2]));

$data[$k1][$k2]=str_replace(" "," ",$data[$k1][$k2]);

$data[$k1][$k2]=str_replace("n","
n",$data[$k1][$k2]);

}

}

else

{

$data[$k1]=nl2br(htmlspecialchars($data[$k1]));

$data[$k1]=str_replace(" "," ",$data[$k1]);

$data[$k1]=str_replace("n","
n",$data[$k1]);

}

}

}

else

{

$data=nl2br(htmlspecialchars($data));

$data=str_replace(" "," ",$data);

$data=str_replace("n","
n",$data);

}

Return $data;

}

}

?>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1018372.htmlTechArticlePHP common form validation class usage examples. This article describes the common PHP form validation class usage examples. Share it with everyone for your reference. The details are as follows: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn