Peter Pik
Peter Pik

Reputation: 11193

XPath works in browser, but not in simple HTML DOM

I'm trying to parse a specific object which contain a certain string. using XPathHelper i've created below XPath which works fine in on the site however when I try to do it programatically. it returns nothing?

What could trigger this

include('simple_html_dom.php');
$gosu_full = file_get_html("http://www.gosugamers.net/counterstrike/news/30998-  cph-wolves-disbands-cs-go-team");

echo $gosu_full->find("//em[contains(text(), 'More content on GosuGamers:')]", 0);

When I just do this

echo $gosu_full->find("//em", 0);

it returns

<em>More content on GosuGamers:</em>

Upvotes: 1

Views: 280

Answers (1)

VolenD
VolenD

Reputation: 3692

You can do it with PHP DOM instead of with simple_html_dom.php as follows:

$doc = new DOMDocument();
// loadHTMLFile generates many warnings, so we want to suppress them
@$doc->loadHTMLFile("http://www.gosugamers.net/counterstrike/news/30998-  cph-wolves-disbands-cs-go-team");

$xpath = new DOMXpath($doc);
$elements = $xpath->query("//em[contains(text(), 'More content on GosuGamers:')]");

echo $elements->item(0)->nodeValue;

However, you do not need [contains(text(), 'More content on GosuGamers:')] at all here, so you can reduce it to just:

$elements = $xpath->query("//em");

and have the same effect.

Upvotes: 1

Related Questions