I trying to get text from div where class = \'review-text\', by using PHP\'s DOM element with following HTML (same structure) and following code.
The following XPath query does what you want. Just replace the argument provided to $xpath->query with the following:
//div[@class="review-text"]
Edit: For easy development, you can test your own XPath query's online at http://www.xpathtester.com/test.
Edit2: Tested this code; it worked perfectly.
<?php
$html = '
<div class="page-wrapper">
<section class="page single-review" itemtype="http://schema.org/Review" itemscope="" itemprop="review">
<article class="review clearfix">
<div class="review-content">
<div class="review-text" itemprop="reviewBody">
Outstanding ...
</div>
</div>
</article>
</section>
</div>
';
$classname = 'review-text';
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$results = $xpath->query("//*[@class='" . $classname . "']");
if ($results->length > 0) {
echo $review = $results->item(0)->nodeValue;
}
?>
Expanding on Frak Houweling answer, it is also possible to use DomXpath to search within a specific DomNode. This can be acheived by passing the contextNode
as a second argument to DomXpath->query
method:
$dom = new DOMDocument;
$dom->loadHTML ($html);
$xpath = new DOMXPath ($dom);
foreach ($xpath->query ("//section[@class='page single-review']") as $section)
{
// search for sub nodes inside each element
foreach ($xpath->query (".//div[@class='review-text']", $section) as $review)
{
echo $review->nodeValue;
}
}
Note that when searching inside nodes you need to use relative paths by adding a dot .
at the beginning of the expression:
"//div[@class='review-text']" // absolute path, search starts from the root element
".//div[@class='review-text']" // relative path, search starts from the provided contextNode