PHP: Parse input form
The code below is used to parse input tags in html form. If you have a script to crawl a website and it does have input form with many inputs , this will save you a lot of time.
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 |
function parse_html_input($html) { $inputs=array(); $regexp = "/<input(.*?)>/is"; if(preg_match_all ("$regexp", $html, $matches)) { foreach($matches[0] as $match) { preg_match('/name=[\"\']{0,1}(\w+)[\"\']{0,1}/i',$match,$result); if(!$result) continue; $value=""; $input_name = $result[1]; if($input_name=="") continue; if($result) { $match=str_replace(">"," >",$match); if(strpos(strtolower($match),"value=\"")!==false) preg_match('/value=[\"\']{0,1}[^"]*[\"\']{0,1}/i',$match,$result2); else preg_match('/value=[^\n\r"]+ /i',$match,$result2); if($result2) { $value= $result2[0]; if(strtolower(substr($value,0,6))=="value=") { $value=substr($value,6,strlen($value)-6); } $value=str_replace("\"","",$value); } } $inputs[$input_name]=$value; } } $form_regexp = "<form.+>"; $form_action=""; if(preg_match_all ("/$form_regexp/i", $html, $matches)) { foreach($matches[0] as $match) { if(strpos(strtolower($match),"action=\"")!==false) { preg_match('/action=[\"\']{0,1}[^\n\r"]*[\"\']{0,1}/i',$match,$result2); $form_action=explode("\"",$result2[0])[1]; } elseif(strpos(strtolower($match),"action=")!==false) { preg_match('/action=[^\n\r"]+ /i',$match,$result2); $form_action=explode("=",$result2[0])[1]; } } } if($form_action!="") { $inputs["form_action"]=$form_action; } return $inputs; } |
Leave a Reply