I am trying to parse a HTML file using DOMDocument class in PHP.
The sample HTM开发者_如何学PythonL file is<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <title></title>
    </head>
    <body>
        <p id="myparagraph"></p>
    </body>
</html>
and I loaded it using
$document = new DOMDocument();
$document->loadHtmlFile("page.html");
Now I try to get the p element by id this way
print_r($document->getElementById("myparagraph"));
This doesn't work for some reason. So what's wrong with this ?
According to comments in the PHP documentation getElementById doesn't really work that well. What you can do is create your own function similar to this:
function getElementById($id)
{
    $xpath = new DOMXPath($this->domDocument);
    return $xpath->query("//*[@id='$id']")->item(0);
}
With thanks to paradox_haze who posted the information in the comments on the PHP docs.
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论