StudioTime
StudioTime

Reputation: 23989

Get sibling variable value from XPath - PHP

I am using the following to extract the number from html using xpath:

$dom = new DOMDocument();
$dom->loadHTML('<span itemprop="priceCurrency" content="GBP"></span><span class="foo" d="bar" itemprop="price">£100</span>');
$xpath = new DOMXPath($dom);
$results = $xpath->query('//*[@itemprop="price"]');
$number = filter_var($results->item(0)->nodeValue, FILTER_SANITIZE_NUMBER_INT);
echo $number;

Question is, how can I get the content variable from <span itemprop="priceCurrency" content="GBP">?

I can use the following to get to the node:

$currency = $xpath->query('//*[@itemprop="priceCurrency"]');

But is there a way to get content value?

Upvotes: 1

Views: 116

Answers (1)

Warren Moore
Warren Moore

Reputation: 458

Yes, using the getAttribute() method:

$currency = $xpath->query('//*[@itemprop="priceCurrency"]')->item(0);
$content = $currency->getAttribute('content');
echo $content; // prints "GBP"

Upvotes: 1

Related Questions