Php Xpath-如何获得特定值


Php Xpath - How to get a specific value?

如何使用php-xpath从下面的字符串中只获取货币值?

<div class="detalheDir2">
<div class="preco">
R$
<script type="text/javascript">document.write(aZ427014992694557())</script>
24,90
</div>
</div>

我只想得到24,90

我正在使用查询

$xpath->query("descendant::div[@class='detalheDir2']/div[@class='preco']/text()", $child);

如果你想要的文本节点总是在这个位置,你可以使用索引,比如:

//div[@class='detalheDir2']/div[@class='preco']/text()[2]

如果它总是在<script>标签之后,你可以写:

//div[@class='detalheDir2']/div[@class='preco']/script/following-sibling::text()

如果它总是最后一个文本节点,你可以写

//div[@class='detalheDir2']/div[@class='preco']/text()[last()]

也许它包含,这一事实可能是关键:

//div[@class='detalheDir2']/div[@class='preco']/text()[contains(., ',')]

所有这些都适用于您的示例,但是您的输入中的差异会使其中任何一个中断。

如果你只知道它在文本节点中的某个地方,并且只有格式将它与其他节点区分开来,那么你最好选择所有的文本节点并迭代它们,并在textContent上与regexp匹配。