Preface
Recent work encounters a requirement that requires all possible elements in the regular matching page, all possible forms of form form, possibly input,action,select,textarea, and so on, this article gives a code example. Interested friends can refer to the study.
The instance code is as follows
Suppose page 1.html of the page source code is:
<! DOCTYPE html>
We need to get to all form forms on this page, and the various form elements contained in each form form, for example: Input,select,textarea and so on.
The matching source code is:
$content = file_get_contents (' 1.html '), $arr _form = Get_page_form_data ($content), if (Empty ($arr _form)) {echo ' Sorry! Not matched to 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 [' inputs ']) {echo '----inputs:<br/> '; foreach ($v [' inputs '] as $key = + $value) {echo '--------N Ame: '. $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/> '; }}}}}}//gets all input in the form form on the page, the TEXTAREA element in the name, value, type, and other property values 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;} Regular Match form tag function regular_form_tags ($string) {$pattern = '/<form (. *?) > (. *?) <\/form>/si '; Preg_match_all ($pattern, $string, $result); return $result;} Regular matches the Action property value of the form tag function regular_form_action ($string) {$pattern = '/action[\s]*?=[\s]*? ( [\'\"]) (.*?) \1/'; if (Preg_match ($pattern, $string, $result)) {return $result [2];} return null;} Regular matches the method property value of the form label function Regular_form_method ($string) {$pattern = '/method[\s]*?=[\s]*? ( [\'\"]) (.*?) \1/'; if (Preg_match ($pattern, $string, $result)) {return $result [2];} return null;} Regular match input tag function regular_inPut_tags ($string) {$pattern = '/<input.*?\/?>/si '; if (Preg_match_all ($pattern, $string, $result)) {return $result; } return null; Regular matches the name attribute value of the input tag function regular_input_name ($string) {$pattern = '/name[\s]*?=[\s]*? ( [\'\"]) (.*?) \1/'; if (Preg_match ($pattern, $string, $result)) {return $result [2];} return null; }//matches the type attribute value of the input label function Regular_input_type ($string) {$pattern = '/type[\s]*?=[\s]*? ( [\'\"]) (.*?) \1/'; if (Preg_match ($pattern, $string, $result)) {return $result [2];} return null;} The value of the regular match input label function regular_input_value ($string) {$pattern = '/value[\s]*?=[\s]*? ( [\'\"]) (.*?) \1/'; if (Preg_match ($pattern, $string, $result)) {return $result [2];} return null; }//matches 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 Name property value of the regular match 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 Name property value of the regular match textarea label function Regular_textarea_value ($string) {$pattern = '/<textarea.*?> (. *?) <\/textarea>/si '; if (Preg_match ($pattern, $string, $result)) {return $result [1];} return null;} Regular Match Select tag function regular_select_tags ($string) {$pattern = '/<select (. *?) > (. *?) <\/select[\s]*?>/si '; Preg_match_all ($pattern, $string, $result); return $result;} The option sub-label function regular_option_tags ($string) {$pattern = '/<option (. *?) of the select tag is matched. >.*?<\/option[\s]*?>/si '; Preg_match_all ($pattern, $string, $result); return $result; }//regular matches the Name property value of the Select Tag function regular_select_name ($string) {$pattern = '/name[\s]*?=[\s]*? ( [\'\"]) (.*?) \1/si '; if (Preg_match ($pattern, $string, $result)) {return $result [2];} return null;} Regular matches the value of the sub-label option of the Select function Regular_option_value ($string) {$pattern = '/value[\s]*?=[\s]*? ([\'\"]) (.*?) \1/si '; if (Preg_match ($pattern, $string, $result)) {return $result [2];} return null;}
The effect is as follows:
This allows us to get all the elements that exist in the form form on any page!