· 1 min read

XPath with HTML in PHP One Liner

Here is a one liner for using XPATH with HTML in PHP

$doc = new DOMXPath(@DOMDocument::loadHTML(file_get_contents("https://www.reddit.com/r/PHP/")));

Now you can use XPATH to query the html..

foreach($doc->query("//a") as $el){
    echo $el->nodeValue . " " . $el->getAttribute("href") . "\n";
}

1 comment

  • Michelle

    Very cool!

Leave a comment