Search code examples
parsingdomdocumentsimpledom

How to parse form elements from html response


        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

        <html xmlns="http://www.w3.org/1999/xhtml">
        <head id="Head1"><title>
            Online-Edu-Help.com
        </title></head>

        <body>
            <form name="form1" method="post" action="Post.aspx" id="form1">
        <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTYxNTMxOTk3MGRkTKiZyXxt1USQVFZyWkCnecSqXls=" />

            <span id="lblError"></span>
            <span id="lblNoError">Accepted: 3231401</span>
            <br /> </form>
        </body>
        </html>

I want to parse the value of lblError and lblNoError from above html response in php.


Solution

  •         <?php
            $html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head id="Head1"><title>
                Online-Edu-Help.com
            </title></head>
    
            <body>
                <form name="form1" method="post" action="Post.aspx" id="form1">
            <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTYxNTMxOTk3MGRkTKiZyXxt1USQVFZyWkCnecSqXls=" />
    
                <span id="lblError"></span>
                <span id="lblNoError">Accepted: 3231401</span>
                <br /> </form>
            </body>
            </html>';
            $dom = new DomDocument();
            $dom->loadHTML($html);
    
            $lblNoError = $dom->getElementById('lblNoError'); 
                echo $lblNoErrorvalue = $lblNoError->nodeValue;
            $lblError = $dom->getElementById('lblError'); 
                echo $lblErrorvalue = $lblError->nodeValue;
            ?>