mk_89
mk_89

Reputation: 2742

regex matching correct text

I am having problems trying to extract the correct value from a text

I want to extract 14.50 from the following text (which is the last decimal number in the string).

string

<span class="ob-pricedetails">Price:</span> &#036;57.71<span style="color: #666666; font-size: 12px;">(&#163;37.61)</span><br/><span style="font-size: 11px; color: #000000;">Shipping (UK):</span>&#036;14.50

I have been trying to use the following regex

regex

(?<=Shipping \(UK\):<\/span>&#163;|&#036;)(.*)

which returns the following result for some strange reason

57.71<span style="color: #666666; font-size: 12px;">(&#163;37.61)</span><br/><span style="font-size: 11px; color: #000000;">Shipping (UK):</span>&#036;14.50

What am I doing wrong? any help would be appreciated.

Upvotes: 0

Views: 68

Answers (2)

Mike Perrenoud
Mike Perrenoud

Reputation: 67948

This should do the trick for you.

[0-9\.]+$

Upvotes: 1

Tchoupi
Tchoupi

Reputation: 14691

This would work:

preg_match('/Shipping \(UK\):<\/span>&#036;([0-9]+\.[0-9]+)/', $html, $matches);

Of course you should listen to everyone suggesting yo use a DOM parser instead of regular expressions.

Upvotes: 2

Related Questions