简体   繁体   English

PHP Xpath-如何获取特定值?

[英]Php Xpath - How to get a specific value?

How can I get just the currency value from a string bellow using php xpath? 如何使用php xpath从字符串波纹管中仅获取货币值?

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

I want to get just 24,90 我只想得到24,90

I'm using the query 我正在使用查询

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

If your desired text node is always in this position, you can use indexing like: 如果所需的文本节点始终处于此位置,则可以使用如下索引:

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

If its always after the a <script> tag you can write: 如果它总是在<script>标记之后,则可以编写:

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

If its always the last text node you can write 如果它始终是最后一个文本节点,则可以编写

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

Maybe the fact that it contains a , could be the key: 也许,它包含了这样的事实,可能是关键:

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

All of these work for your example, however variance in your input will make any of these break. 所有这些对于您的示例都是有效的,但是输入中的差异将导致这些中断。

If you only know that it's there somewhere among the text nodes and only the format sets it apart from the others, you might be better off selecting all the text nodes and iterate through them, and matching with regexps on textContent . 如果您仅知道它在文本节点之间的某个地方,并且只有格式将其与其他节点分开,则最好选择所有文本节点并对其进行遍历,并与textContent上的正则表达式匹配。

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

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