The reason it's showing only the first one now is because you have selected only for ->item( 0)
, you need to loop through them. Also, to print the elements inside the tag, you use nodeValue
(you have tried textContent
which doesn't exist).
The following code prints 10 reviews in table with the rating (stars), headlines and content:
$url = "https://www.trustpilot.co.uk/review/www.currys.co.uk";
$html = file_get_contents( $url);
libxml_use_internal_errors( true);
$doc = new DOMDocument; $doc->loadHTML( $html);
$xpath = new DOMXpath( $doc);
//get all ratings where <meta itemprop="ratingValue">
$ratings = $xpath->query('//meta[@itemprop="ratingValue"]');
//get all headings where <h3 class="review-title en h4">
$headings = $xpath->query( '//h3[@class="review-title en h4"]');
//get all content
$node = $xpath->query( '//div[@itemprop="reviewBody"][@class="review-body"]');
$table = "<table border=1>";
for($i=0;$i<10;$i++){
$table .= '<tr>
<td>Star: '.str_repeat("*",$ratings->item($i)->getAttribute('content')).'</tr>
<td>'.$headings->item($i)->nodeValue.'</tr>
<td>'.$node->item($i)->nodeValue.'</tr>
</tr>';
}
$table .= '</table>';
echo $table;