前言
最近工作中遇到一个需求,需要在正则匹配页面中,所有可能存在的 form 表单的元素,可能有 input,action,select,textarea等等所有可能的元素,本文给出一个代码示例。感兴趣的朋友们可以参考学习。
实例代码如下
假设页面 1.html 的网页源代码是:
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
|
<!DOCTYPE html>
< html >
< head >
< meta charset = "utf-8" />
< title >一个含有 form 表单的登录注册页面</ title >
< style type = "text/css" >
*{padding:0;margin:0;font-size:12px;}
.tbl{margin:20px auto;border:1px solid #AACCEE;padding:30px 30px;border-radius:4px;}
.tbl td{line-height:30px;vertical-align:top;}
textarea{resize:none;height:60px;}
</ style >
</ head >
< body >
< form action = "register.php" method = "post" >
< input type = "hidden" name = "cookie" value = "SJKjki80KJ8jkl2" />
< table cellpadding = "0" cellspacing = "0" class = "tbl" >
< tr >
< th colspan = "2" >注册</ th >
</ tr >
< tr >
< td >昵称:</ td >
< td >< input type = "text" name = 'name' required = "" /></ td >
</ tr >
< tr >
< td >密码:</ td >
< td >< input name = "password" type = "password" required = "required" /></ td >
</ tr >
< tr >
< td >年龄:</ td >
< td >< input name = "age" value = "22" required = "required" ></ td >
</ tr >
< tr >
< td >性别:</ td >
< td >
< input type = "radio" name = "sex" value = "1" /> 男
< input type = "radio" name = "sex" value = "0" /> 女
</ td >
</ tr >
< tr >
< td >地区:</ td >
< td >
< select name = "area" >
< option value = "jiangsu" >江苏</ option >
< option value = "shandong" >山东</ option >
< option value = "fujian" >福建</ option >
< option value = "beijing" >北京</ option >
</ select >
</ td >
</ tr >
< tr >
< td >城市:</ td >
< td >
< select name = "city" >
< option value = "qingdao" >青岛</ option >
< option value = "longyan" >龙岩</ option >
< option value = "beijing" >北京</ option >
< option value = "wuxi" >无锡</ option >
</ select >
</ td >
</ tr >
< tr >
< td >兴趣:</ td >
< td >
< input type = "checkbox" name = "xingqu[]" value = "1" > 篮球
< input type = "checkbox" name = "xingqu[]" value = "2" > 足球
< input type = "checkbox" name = "xingqu[]" value = "3" > 跳高
</ td >
</ tr >
< tr >
< td >照片:</ td >
< td >< input type = "file" name = "photo" /></ td >
</ tr >
< tr >
< td >简介:</ td >
< td >< textarea name = "summary" required = "" >这里填入个人简介</ textarea ></ td >
</ tr >
< tr >
< td >备注:</ td >
< td >< textarea name = "remark" required = "" >1</ textarea ></ td >
</ tr >
< tr >
< td colspan = "2" >< input type = "submit" name = "register" value = "注册" ></ td >
</ tr >
</ table >
</ form >
< form action = "login.php" method = "post" >
< table cellpadding = "0" cellspacing = "0" class = "tbl" >
< tr >
< th colspan = "2" >登录</ th >
</ tr >
< tr >
< td >昵称:</ td >
< td >< input type = "text" name = 'name' required = "required" /></ td >
</ tr >
< tr >
< td >密码:</ td >
< td >< input name = "password" type = "password" required = "required" /></ td >
</ tr >
< tr >
< td >备注:</ td >
< td >< textarea name = "remark" required = "" >2</ textarea ></ td >
</ tr >
< tr >
< td colspan = "2" >< input type = "submit" name = "register" value = "登录" ></ td >
</ tr >
</ table >
</ form >
</ body >
</ html >
|
我们需要获取到这个页面所有 form 表单,及每个 form 表单所包含的各类表单元素,例如:input,select,textarea等等。
匹配的源代码是:
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
$content = file_get_contents ( '1.html' );
$arr_form = get_page_form_data( $content );
if ( empty ( $arr_form ))
{
echo '抱歉!未匹配到 form 表单元素' ;
} else
{
foreach ( $arr_form as $k => $v )
{
echo 'form' .( $k +1). ':<br />' ;
if (! empty ( $v [ 'action' ]))
{
echo '----action:<br />' ;
echo '--------' . $v [ 'action' ]. '<br />' ;
}
if (! empty ( $v [ 'method' ]))
{
echo '----method:<br />' ;
echo '--------' . $v [ 'method' ]. '<br />' ;
}
if (! empty ( $v [ 'inputs' ]))
{
echo '----inputs:<br />' ;
foreach ( $v [ 'inputs' ] as $key => $value )
{
echo '--------name:' . $value [ 'name' ]. ' type:' . $value [ 'type' ]. ' value:' . $value [ 'value' ]. '<br />' ;
}
}
if (! empty ( $v [ 'textarea' ]))
{
echo '----textarea:<br />' ;
foreach ( $v [ 'textarea' ] as $key => $value )
{
echo '--------name:' . $value [ 'name' ]. ' value:' . $value [ 'value' ]. '<br />' ;
}
}
if (! empty ( $v [ 'select' ]))
{
echo '----select:<br />' ;
for ( $m = 0; $m < count ( $v [ 'select' ]); $m ++)
{
echo '--------name:' . $v [ 'select' ][ $m ][ 'name' ]. '<br />' ;
if (! empty ( $v [ 'select' ][ $m ][ 'option' ]))
{
foreach ( $v [ 'select' ][ $m ][ 'option' ] as $key => $value )
{
echo '------------value:' . $value . '<br />' ;
}
}
}
}
}
}
// 获取页面中的 form 表单中的所有 input、textarea 元素中 name、value、type 等属性值
function get_page_form_data( $content )
{
$arr_form = array ();
$form = regular_form_tags( $content );
for ( $i = 0; $i < count ( $form [0]); $i ++)
{
$arr_form [ $i ][ 'action' ] = regular_form_action( $form [1][ $i ]);
$arr_form [ $i ][ 'method' ] = regular_form_method( $form [1][ $i ]);
$input = regular_input_tags( $form [2][ $i ]);
for ( $j = 0; $j < count ( $input [0]); $j ++)
{
$arr_form [ $i ][ 'inputs' ][ $j ][ 'name' ] = regular_input_name( $input [0][ $j ]);
$arr_form [ $i ][ 'inputs' ][ $j ][ 'type' ] = regular_input_type( $input [0][ $j ]);
$arr_form [ $i ][ 'inputs' ][ $j ][ 'value' ] = regular_input_value( $input [0][ $j ]);
}
$textarea = regular_textarea_tags( $form [2][ $i ]);
for ( $k = 0; $k < count ( $textarea ); $k ++)
{
$arr_form [ $i ][ 'textarea' ][ $k ][ 'name' ] = regular_textarea_name( $textarea [ $k ]);
$arr_form [ $i ][ 'textarea' ][ $k ][ 'value' ] = regular_textarea_value( $textarea [ $k ]);
}
$select = regular_select_tags( $form [2][ $i ]);
for ( $l = 0; $l < count ( $select [0]); $l ++)
{
$arr_form [ $i ][ 'select' ][ $l ][ 'name' ] = regular_select_name( $select [1][ $l ]);
$option = regular_option_tags( $select [2][ $l ]);
for ( $n = 0; $n < count ( $option [ $l ]); $n ++)
{
$arr_form [ $i ][ 'select' ][ $l ][ 'option' ][ $n ] = regular_option_value( $option [ $l ][ $n ]);
}
}
}
return $arr_form ;
}
// 正则匹配 form 标签
function regular_form_tags( $string )
{
$pattern = '/<form(.*?)>(.*?)<\/form>/si' ;
preg_match_all( $pattern , $string , $result );
return $result ;
}
// 正则匹配 form 标签的 action 属性值
function regular_form_action( $string )
{
$pattern = '/action[\s]*?=[\s]*?([\'\"])(.*?)\1/' ;
if (preg_match( $pattern , $string , $result ))
{
return $result [2];
}
return null;
}
// 正则匹配 form 标签的 method 属性值
function regular_form_method( $string )
{
$pattern = '/method[\s]*?=[\s]*?([\'\"])(.*?)\1/' ;
if (preg_match( $pattern , $string , $result ))
{
return $result [2];
}
return null;
}
// 正则匹配 input 标签
function regular_input_tags( $string )
{
$pattern = '/<input.*?\/?>/si' ;
if (preg_match_all( $pattern , $string , $result ))
{
return $result ;
}
return null;
}
// 正则匹配 input 标签的 name 属性值
function regular_input_name( $string )
{
$pattern = '/name[\s]*?=[\s]*?([\'\"])(.*?)\1/' ;
if (preg_match( $pattern , $string , $result ))
{
return $result [2];
}
return null;
}
// 正则匹配 input 标签的 type 属性值
function regular_input_type( $string )
{
$pattern = '/type[\s]*?=[\s]*?([\'\"])(.*?)\1/' ;
if (preg_match( $pattern , $string , $result ))
{
return $result [2];
}
return null;
}
// 正则匹配 input 标签的 value 属性值
function regular_input_value( $string )
{
$pattern = '/value[\s]*?=[\s]*?([\'\"])(.*?)\1/' ;
if (preg_match( $pattern , $string , $result ))
{
return $result [2];
}
return null;
}
// 正则匹配 textarea 标签
function regular_textarea_tags( $string )
{
$pattern = '/(<textarea.*?>.*?<\/textarea[\s]*?>)/si' ;
if (preg_match_all( $pattern , $string , $result ))
{
return $result [1];
}
return null;
}
// 正则匹配 textarea 标签的 name 属性值
function regular_textarea_name( $string )
{
$pattern = '/name[\s]*?=[\s]*?([\'\"])(.*?)\1/si' ;
if (preg_match( $pattern , $string , $result ))
{
return $result [2];
}
return null;
}
// 正则匹配 textarea 标签的 name 属性值
function regular_textarea_value( $string )
{
$pattern = '/<textarea.*?>(.*?)<\/textarea>/si' ;
if (preg_match( $pattern , $string , $result ))
{
return $result [1];
}
return null;
}
// 正则匹配 select 标签
function regular_select_tags( $string )
{
$pattern = '/<select(.*?)>(.*?)<\/select[\s]*?>/si' ;
preg_match_all( $pattern , $string , $result );
return $result ;
}
// 正则匹配 select 标签的 option 子标签
function regular_option_tags( $string )
{
$pattern = '/<option(.*?)>.*?<\/option[\s]*?>/si' ;
preg_match_all( $pattern , $string , $result );
return $result ;
}
// 正则匹配 select 标签的 name 属性值
function regular_select_name( $string )
{
$pattern = '/name[\s]*?=[\s]*?([\'\"])(.*?)\1/si' ;
if (preg_match( $pattern , $string , $result ))
{
return $result [2];
}
return null;
}
// 正则匹配 select 的子标签 option 的 value 属性值
function regular_option_value( $string )
{
$pattern = '/value[\s]*?=[\s]*?([\'\"])(.*?)\1/si' ;
if (preg_match( $pattern , $string , $result ))
{
return $result [2];
}
return null;
}
|
运行效果如下图所示:
这样我们就可以实现获取任意一个页面中的 form 表单所有存在的元素啦!
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用PHP能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。