sanjeev
sanjeev

Reputation: 4621

Getting HTML element in PHP

I am struck in a problem, i want a small amount of data from a site, but this data is present in different pages.

Hence i used curl request to call this pages

Problem:

curl request gives my a html page, and finding that small amount of content is difficult with php as, It is very simple to move through the HTML Dom with JavaScript(jquery). Can i move with same ease with php in HTML Dom

any help would be appreciated , please provide me link of such site also

I am not asking for all the coding , but just a library or link will do

if there is any trick it will also do , a small example like fetching element with ID would be of great help

Thanks

Upvotes: 1

Views: 118

Answers (1)

MueR
MueR

Reputation: 977

Yes you can. You can use DomDocument and DomXPath to navigate your HTML tree and search it.

$doc = new DOMDocument();
$doc->loadHTML($yourHtml); // HTML as string, use loadHTMLFile() to load a file.
$xpath = new DOMXpath($doc);
$elements = $xpath->query("*/div[@id='yourTagIdHere']");
if (empty($elements) || $elements->length == 0)
    echo "Not found";
else
{
    // $elements now contains a DOMNodeList of matching elements
    // which you can foreach() through.
}

Upvotes: 4

Related Questions