Search code examples
phpxpathdomdocument

Get part go text with domDocument in PHP


I've this HTMl coming from a file_get_contents:

<div class="attractions-attraction-filtered-common-ListingsHeader__listingsCount--PflJ1">
    <span>We found <b>10&nbsp;results</b> for you.</span>
</div>

How can I get the number of results (i.e.: 10)?

Note, that the part PflJ1 is something random.

This is what I tried:

$page = file_get_contents($url);

$dom = new DOMDocument();
$dom->loadHTML($html);
$xp = new DOMXpath($dom);

$activitiesNb = $xp->query('//div/span/text()');
$activitiesNb = $activitiesNb->nodeValue;
echo $activitiesNb;

But it does not work.

What I'm missing please ?

Thanks.


Solution

  • Using evaluate():

    $results = $xp->evaluate('string(//span[contains(., "We found ")]/b/text())');