Current Location: Home> Function Categories> preg_match_all

preg_match_all

Perform global regular expression matching
Name:preg_match_all
Category:Regular processing PCRE
Programming Language:php
One-line Description:Find all matches for patterns in strings.

Definition and usage

preg_match_all() function returns the number of matches for the pattern found in the string and fills the variable with the found match.

Example

Example 1

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

Example 2

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

Similar Functions
Popular Articles