Written by Sean Behan on Tue Nov 14th 2017

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";
}

Tagged with..
#php #xpath #xml #html #one liner

Just finishing up brewing up some fresh ground comments...