This article mainly introduces PHP's method of using regular expressions to match the types and attribute values of all elements in form. This article provides the complete example code. you can refer to it for more information. let's take a look at it.
Preface
Recently, I encountered a requirement that all possible form elements, such as input, action, select, and textarea, must be included in the regular expression matching page, this section provides a sample code. Interested friends can refer to learning.
The instance code is as follows:
Assume that the source code of page 1.html is:
A logon registration page containing form
We need to obtain all form forms on this page and various form elements contained in each form, such as input, select, and textarea.
The matched source code is:
$ Content = file_get_contents('1.html '); $ arr_form = get_page_form_data ($ content); if (empty ($ arr_form) {echo' Sorry! The form element ';} else {foreach ($ arr_form as $ k => $ v) {echo 'form'. ($ k + 1 ).':
'; If (! Empty ($ v ['action']) {echo '---- action:
'; Echo' -------- '. $ v ['action'].'
';} If (! Empty ($ v ['method']) {echo '---- method:
'; Echo' -------- '. $ v ['method'].'
';} If (! Empty ($ v ['inputs']) {echo '---- inputs:
'; Foreach ($ v ['inputs'] as $ key => $ value) {echo' -------- name :'. $ value ['name']. 'type :'. $ value ['type']. 'value :'. $ value ['value'].'
';}} If (! Empty ($ v ['textea ']) {echo' ---- textarea:
'; Foreach ($ v ['textarea'] as $ key => $ value) {echo '-------- name :'. $ value ['name']. 'value :'. $ value ['value'].'
';}} If (! Empty ($ v ['select']) {echo '---- select:
'; For ($ m = 0; $ m <count ($ v ['select']); $ m ++) {echo' -------- name :'. $ v ['select'] [$ m] ['name'].'
'; If (! Empty ($ v ['select'] [$ m] ['option']) {foreach ($ v ['select'] [$ m] ['option'] as $ key => $ value) {echo '------------ value :'. $ value.'
';}}}}}// Obtain the function get_page_form_data ($ content) attribute values of name, value, and type in all input and textarea elements in the form on the page) {$ 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] ['inputputs'] [$ j] ['name'] = regular_input_name ($ input [0] [$ j]); $ arr_form [$ I] ['effects'] [$ j] ['type'] = regular_input_type ($ input [0] [$ j]); $ arr_form [$ I] ['effects'] [$ 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;} // regular expression matching form tag function regular_form_tags ($ string) {$ pattern = '/
(.*?) <\/Form>/si'; preg_match_all ($ pattern, $ string, $ result); return $ result ;} // function regular_form_action ($ string) {$ pattern = '/action [\ s] *? = [\ S] *? ([\ '\ "]) (. *?) \ 1/'; if (preg_match ($ pattern, $ string, $ result) {return $ result [2];} return null ;} // function regular_form_method ($ string) {$ pattern = '/method [\ s] *? = [\ S] *? ([\ '\ "]) (. *?) \ 1/'; if (preg_match ($ pattern, $ string, $ result) {return $ result [2];} return null ;} // regular_input_tags ($ string) {$ pattern = '/
/Si'; if (preg_match_all ($ pattern, $ string, $ result) {return $ result;} return null ;} // function regular_input_name ($ string) {$ pattern = '/name [\ s] *? = [\ S] *? ([\ '\ "]) (. *?) \ 1/'; if (preg_match ($ pattern, $ string, $ result) {return $ result [2];} return null ;} // function regular_input_type ($ string) {$ pattern = '/type [\ s] *? = [\ S] *? ([\ '\ "]) (. *?) \ 1/'; if (preg_match ($ pattern, $ string, $ result) {return $ result [2];} return null ;} // function regular_input_value ($ string) {$ pattern = '/value [\ s] *? = [\ S] *? ([\ '\ "]) (. *?) \ 1/'; if (preg_match ($ pattern, $ string, $ result) {return $ result [2];} return null ;} // regular expression matching textarea tag function regular_textarea_tags ($ string) {$ pattern = '/(
.*? <\/Textarea [\ s] *?>) /Si'; if (preg_match_all ($ pattern, $ string, $ result) {return $ result [1];} return null ;} // the regular expression matches the name attribute value of the textarea label. function regular_textarea_name ($ string) {$ pattern = '/name [\ s] *? = [\ S] *? ([\ '\ "]) (. *?) \ 1/si'; if (preg_match ($ pattern, $ string, $ result) {return $ result [2];} return null ;} // the regular expression matches the name attribute value of the textarea label. function regular_textarea_value ($ string) {$ pattern = '/
(.*?) <\/Textarea>/si'; if (preg_match ($ pattern, $ string, $ result) {return $ result [1];} return null ;} // regular expression match select tag function regular_select_tags ($ string) {$ pattern = '/
(.*?) <\/Select [\ s] *?> /Si'; preg_match_all ($ pattern, $ string, $ result); return $ result;} // The option Sub-tag function regular_option_tags ($ string) of the regular expression matching select tag) {$ pattern = '/
.*? <\/Option [\ s] *?> /Si'; preg_match_all ($ pattern, $ string, $ result); return $ result;} // the name attribute value function regular_select_name ($ string) of the regular match select tag) {$ pattern = '/name [\ s] *? = [\ S] *? ([\ '\ "]) (. *?) \ 1/si'; if (preg_match ($ pattern, $ string, $ result) {return $ result [2];} return null ;} // The value attribute value function regular_option_value ($ string) {$ pattern = '/value [\ s] *? = [\ S] *? ([\ '\ "]) (. *?) \ 1/si'; if (preg_match ($ pattern, $ string, $ result) {return $ result [2];} return null ;}
Shows the running effect:
In this way, we can obtain all the elements that exist in the form on any page!
For more articles about the type and attribute values of all elements in the form matching with regular expressions in PHP, refer to PHP!