preg_match仅匹配单个匹配项

时间:2016-07-05 23:07:20

标签: php regex

我有preg_match只匹配一次出现

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

以下是数据:

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

当我在print_r$match时,我得到以下内容

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

我应该将$2,999.00$2,249.00存储在$match

1 个答案:

答案 0 :(得分:4)

您需要改为使用preg_match_all

$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);

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"
  }
}