Monday, January 28, 2013

DOM Xpath Get href attribute and text value of an element using php


<?php
$url=”http://www.yourdomain.com&#8221;;
$html = file_get_contents($ur]);
//Parse it. Here we use loadHTML as a static method
//to parse the HTML and create the DOM object in one go.
@$dom = DOMDocument::loadHTML($html);

//Init the XPath object
$xpath = new DOMXpath($dom);

//Query the DOM Using DIV ID
$links = $xpath->query( ‘//a[@id = "yourtargetedDIVid "]‘ );

//Query the DOM Using Class Name
$links = $xpath->query( ‘//a[@class = "yourtargetedClassName"]‘ );
foreach ($links as $link) {
echo $link->getAttribute(‘href’);
}
?>

No comments:

Post a Comment