1

I have a string that, from a file_get_contents() call, contains something like:

    <span class="cb_price_countdown cb_lot_price_1439066">$40.65</span>

And I would like the extract the price 40.65. I am not familiar with regex or preg_match so I'm having a hard time. So far I have tried:

    $pattern = "/\\\$?((\d{1,3}(,\d{3})*)|(\d+))(\.\d{2})?$/";
    preg_match ($pattern, $subject, $matches);
    print_r ($matches);

Which is not returning anything useful, and I've tried:

    $pattern = "/[\d+|\d+,\d+]\.\d{0,2}/";

but it's the same story. Can somebody please tell me the correct preg_match pattern that I'm looking for?

Thank you,
Justin

JuJoDi
  • 14,627
  • 23
  • 80
  • 126
  • don't use regular expressions for this. Don't steal content from other sites. –  Feb 17 '13 at 19:34
  • 2
    [Don't use regexes for parsing HTML](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – John Conde Feb 17 '13 at 19:34

2 Answers2

5

use this:

preg_match ( '/\$(\d+\.\d+)/', $subject, $matches );
Boynux
  • 5,958
  • 2
  • 21
  • 29
1
$result = array();
$str = '<span class="cb_price_countdown cb_lot_price_1439066">$40.65</span>';
preg_match('/<span[^>]*>\$(.*)<\/span>/', $str, $result);
var_dump($result);

yields:

array(2) { [0]=> string(61) "$40.65<" [1]=> string(5) "40.65" }
zero323
  • 322,348
  • 103
  • 959
  • 935
netdigger
  • 3,659
  • 3
  • 26
  • 49