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
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> ';
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 ?