我使用以下内容从xtml中使用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;
问题是,如何从content
获取<span itemprop="priceCurrency" content="GBP">
变量?
我可以使用以下内容进入节点:
$currency = $xpath->query('//*[@itemprop="priceCurrency"]');
但有没有办法获得content
价值?
答案 0 :(得分:1)
是的,使用getAttribute()
方法:
$currency = $xpath->query('//*[@itemprop="priceCurrency"]')->item(0);
$content = $currency->getAttribute('content');
echo $content; // prints "GBP"