unixmiah unixmiah - 5 months ago 16
PHP Question

preg_match is only matching single occurrence

I have a

preg_match
that's only matching one occurrence

preg_match('/(\$[0-9,]+(\.[0-9]{2})?)/', $lines[0], $match);


Here is the data:

<strong>Apr- May Price: </strong>Adult: $2,999.00 Children: $2,249.00 <br />


When I do a
print_r
on
$match
I get the following

Array ( [0] => $2,999.00 [1] => $2,999.00 [2] => .00 )


I should be getting
$2,999.00
and
$2,249.00
stored in the
$match

Answer

You need to use preg_match_all instead.

$string = '<strong>Apr- May Price: </strong>Adult: $2,999.00 Children: $2,249.00 <br />';
preg_match_all('/(\$[0-9,]+(\.[0-9]{2})?)/', $string, $match);

And the result of a var_dump($match);:

array(3) {
  [0]=>
  array(2) {
    [0]=>
    string(9) "$2,999.00"
    [1]=>
    string(9) "$2,249.00"
  }
  [1]=>
  array(2) {
    [0]=>
    string(9) "$2,999.00"
    [1]=>
    string(9) "$2,249.00"
  }
  [2]=>
  array(2) {
    [0]=>
    string(3) ".00"
    [1]=>
    string(3) ".00"
  }
}