1
<root>
<td align="left"><a title="Testing" href="www.test.com/product/">$129.00</a></td><td valign="bottom">Testing</td></root>

How do I go about selecting the 129.00 in the above example? I don't want the $.

Currently I have preg_match('/\$.{6}/') to select this. This is generally ok, but whenever it becomes a 4 digit string, it fails. Eg. $50.00

Thanks.

3 Answers 3

4

As many will tell you, don't use regex to parse html/xml structures. But if you really want or have to use regex, try this pattern:

\$([0-9\.]+)

for alternatives look here:

http://php.net/manual/en/book.simplexml.php

http://www.php.net/manual/en/book.dom.php

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

5 Comments

Yeah, I know I should use DOM/XPath, but its a really simple document with a few lines of code... really cant be stuffed using extra stuff. Thanks!
Then I hope the pattern will work for you. But be aware, that it will not match if you have some characters others than 0-9 and .. Maybe adding chars for a thousands separator will be needed. (See: php.net/manual/en/regexp.reference.character-classes.php)
It does.. preg_match('/\$([0-9\.]+)/', $yourString, $matches); and do $matches[1] returns 129 or..how are you using it with other rows?
@tradybix ohh, my bad. It does work too, I accidentally removed one of the quotes ('). Thanks. However it does not remove the $.
preg_match is not the correct function if you want to remove something, look at preg_replace for this. (php.net/manual/en/function.preg-replace.php)
1

Try this regex, got it from here: Link

 \$\d+\.\d{2}

1 Comment

Doesn't seem to work... pastebin.com/WqEUKFNn and SQL result (id 29): img.acianet.co.uk/i/sqlfail.jpg
0

replace all "foo" between two HTML tags using REGEX (PHP code)

<?php
$string ='<root>
<td align="left"><a title="Digitan Technology: Click to see item" href="www.digitan.com.au/home.php?cat=2655" target="retailer">$115.20</a></td><td valign="bottom">AMD PHENOM X4 Quad Core 9650 CPU, 2.3GHz (95W), 4MB Cache, Sockets AM2+/AM2</td></root>';
preg_match('/\$([0-9\.]+)(?=(?:.(?!<a))*<\/a>)/', $string, $match);
echo $match[1];
?>

Comments

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.