0

How can I get just the currency value from a string bellow using 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

I'm using the query

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

1 Answer 1

1

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:

//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.

Sign up to request clarification or add additional context in comments.

2 Comments

The problem is when I use text() I receive just R$ if I remove the text() I receive R$ocument.write(aZ427014992694557())
I'm not sure how you call this exactly, see this demo, this is how i would do it. The thing you get back from $xpath->query() is a DOMNodeList instance, if it contains more than one element you can use foreach to get to the individual DOMNodes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.