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.

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

Your email address will not be published. Required fields are marked *