PHP uses regular expressions to match the instance code of the type and attribute values of all elements in the form. The form Attribute Value

Source: Internet
Author: User

PHP uses regular expressions to match the instance code of the type and attribute values of all elements in the form. The form Attribute Value

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:

<! DOCTYPE html> 

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 ). ': <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 ['inputputs']) {echo '---- inputs: <br/>'; foreach ($ v ['inputputs'] as $ key => $ value) {echo '-------- name :'. $ value ['name']. 'Type :'. $ value ['type']. 'value :'. $ value ['value']. '<br/>';} if (! Empty ($ v ['textea ']) {echo' ---- textarea: <br/> '; foreach ($ v ['textea'] 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/>' ;}}}}// obtain the function get_page_form_data ($ content) of the property 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] ['inputs'] [$ j] ['value'] = regular_input_value ($ input [0] [$ j]);} $ textarea = regular_texta Rea_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 = r Egular_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 (. *?)> (.*?) <\/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 = '/<input. *? \/?> /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. *?>. *? <\/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 ;} // Regular Expression matches the name attribute value function regular_textarea_value ($ string) {$ pattern = '/<textarea. *?> (.*?) <\/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 (. *?)> (.*?) <\/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 (. *?)>. *? <\/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!

Summary

The above is all the content of this article. I hope the content of this article will help you learn or use PHP. If you have any questions, you can leave a message, thank you for your support.

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.