19

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.

However this doesn't seem to work

  1. HTML

    $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>
    ';
    
  2. PHP Code

        $classname = 'review-text';
        $dom = new DOMDocument;
        $dom->loadHTML($html);
        $xpath     = new DOMXPath($dom);
        $results = $xpath->query("//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
    
        if ($results->length > 0) {
            echo $review = $results->item(0)->nodeValue;
        }
    

The XPATH syntax to select element by Class is provided at this Blog

I have tried many example from StackOverflow, online tutorials, but none seems to work. Am I missing something ?

Abhishek Madhani
  • 1,155
  • 4
  • 16
  • 26

2 Answers2

31

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

?>
Frank Houweling
  • 595
  • 5
  • 14
7

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
Community
  • 1
  • 1
razz
  • 9,770
  • 7
  • 50
  • 68