php - preg_match_all matches array -


i have this

$matches = array(); preg_match_all('/(`.+`)(\s+as\s+`.+`)?/i', '`foo` `bar`', $matches); print_r($matches); 

the result is

array ( [0] => array     (         [0] => `foo` `bar`     )  [1] => array     (         [0] => `foo` `bar`     )  [2] => array     (         [0] =>      )  ) 

so, question why don't have ' `bar`' in $matches[2][0]? (if remove '?' symbol regex, i'll it, need '?' :))

quantifiers + greedy default if first 1 can match so. making non-greedy should job:

preg_match_all('/(`.+?`)(\s+as\s+`.+`)?/i', '`foo` `bar`', $matches); 

and way, $matches = array(); not necessary - variable written preg_match_all not need initialized/defined before.

php > preg_match_all('/(`.+?`)(\s+as\s+`.+`)?/i', '`foo` `bar`', $matches); php > print_r($matches); array (     [0] => array         (             [0] => `foo` `bar`         )      [1] => array         (             [0] => `foo`         )      [2] => array         (             [0] =>  `bar`         )  ) 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -