9951 explained code solutions for 126 technologies


php-regexHow to find all matches when using regex in PHP?


To find all matches when using regex in PHP, you can use the preg_match_all() function. This function takes two parameters: the regular expression pattern and the string to search. It returns an array of all matches found.

Example code

$string = 'This is a string';
$pattern = '/\w+/';

preg_match_all($pattern, $string, $matches);

print_r($matches);

Output example

Array
(
    [0] => Array
        (
            [0] => This
            [1] => is
            [2] => a
            [3] => string
        )

)

Code explanation

  • $string: The string to search.
  • $pattern: The regular expression pattern.
  • preg_match_all(): The function used to find all matches.
  • $matches: The array of all matches found.
  • print_r(): The function used to print the array of matches.

Helpful links

Edit this code on GitHub