Search code examples
phphtmldomdomdocument

php DOMDocument()->getAttribute() not working


I want to get the value of href attribute of a tag from HTML inside a string.

I have made a PHP fiddle here because the string is too long.

Error:

PHP Parse error:  syntax error, unexpected 'undefined' (T_STRING) in...

Solution

  • In php Sandbox your code works.

    However, you forgot < at the beginning of a tag.

    <?php
    $string = '<!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" xml:lang="en" lang="en">
                <head>
                </head>
                <body onclick="on_body_click()" text="#000000" alink="#FF0000" link="#0000FF" vlink="#800080"> 
                 <a href="/cgi-bin/new_get_recorded.cgi?l_doc_ref_no=7506389&amp;COUNTY=san francisco&amp;YEARSEGMENT=current&amp;SEARCH_TYPE=DETAIL_N" title="Document Details">Show Name Detail</a> 
                    </body>
        </html>';
    
    $doc = new DOMDocument();
    $doc->loadHTML($string);
    $selector = new DOMXPath($doc);
    $result = $selector->query('//a[@title="Document Details"]');
    $url = $result[0]->getAttribute('href');
    echo $url;
    

    In $url you have the href value (printed out).

    It seems you have problem with the string and use of ' and ".

    If you start the $string with ' you cannot use it inside. You can use ' just at the end to close the php variable ';;

    You have three solutions:

    1. substitute ' with " inside the string representing your html;
    2. Use \' instead of only ' inside the string representing your html. This tells to php that the string is not finished yet but that ' represents the string content;
    3. heredoc syntax;

    For example with the first approach we have:

    $string = ' Inside the string you should use just this type of apostrophe " ';