preg_match_all
Perform global regular expression matching
preg_match_all()
function returns the number of matches for the pattern found in the string and fills the variable with the found match.
Find all "ain" occurrences in the string:
<?php $str = "The rain in SPAIN falls mainly on the plains." ; $pattern = "/ain/i" ; if ( preg_match_all ( $pattern , $str , $matches ) ) { print_r ( $matches ) ; } ?>
Try it yourself
Use PREG_PATTERN_ORDER to set the structure of the matches array. In this example, each element in the matches array has all matches from one of the regular expression groups.
<?php $str = "abc ABC" ; $pattern = "/((a)b)(c)/i" ; if ( preg_match_all ( $pattern , $str , $matches , PREG_PATTERN_ORDER ) ) { print_r ( $matches ) ; } ?>
Try it yourself