user1661677 user1661677 - 2 years ago 102
PHP Question

How to isolate a specific nested value in complex DOM element string?

I'm looking to return the value that is nested in the an element containing an href value with

in it (in this example, it is
7 goals
). Any help?

$html_string = '<div class="foo"><a href="../john-smith.html"><span>7 goals</span></a></div><div class="foo2"><h2><a href="../adam-green.html">8 points</a></h2></div>';
echo $isolated_character;

Answer Source

As a variant, you Can use class DomDocument and its Xpath

$d = new DomDocument();
$x = new DomXpath($d);
echo $res = $x->evaluate('string(//a[contains(@href, "john")]/span)'); // 7 goals
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download