简体   繁体   English

从XPath获取同级变量值-PHP

[英]Get sibling variable value from XPath - PHP

I am using the following to extract the number from html using xpath: 我正在使用以下内容使用xpath从html提取数字:

$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"> ? 问题是,如何从<span itemprop="priceCurrency" content="GBP">获取content变量?

I can use the following to get to the node: 我可以使用以下内容到达该节点:

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

But is there a way to get content value? 但是有没有办法获得content价值?

Yes, using the getAttribute() method: 是的,使用getAttribute()方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM