前言
最近工作中遇到一個需求,需要在正則匹配頁面中,所有可能存在的 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能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。