PHP - How to use preg_match() and preg_match_all()  screenshot

PHP - How to use preg_match() and preg_match_all()

PHP - How to use preg_match() and preg_match_all()

Hi all! Today we'll learn how to use regular expressions in php and see two functions that will help us a lot: preg_match() and preg_match_all().

Regular expressions are powerful tools made to help us make researches, form validations or replacement in a very flexible way by using patterns. You may for example need to know if a string contains a word beginning by "Jun", or ending by "ior" or something much more complicated like replacing all substrings starting by one or two digit, then a space, then only 3 letters, then a space then ending with four or two digit (a date for example) whatever these digits or letters are. They are really helpful when coming to validation as you don't know by advance what will be the inputs, you only know what format you want them to be in.

I will assume in this tutorial that you know how to write regular expressions and focus more on the two functions.

preg_match()

Ok guys, here is the syntax of the function :

<?php
intpreg_match(stringpattern, stringstring[, array $pattern_array], [, int $flags[, int $offset]]]);
?>

preg_match will basically tell you if a pattern is found or not within a string. Like so:

<?php
echo ('Example 3 ');
$pattern = "/^learn[a-zA-Z0-9\s\-_]*regexp$/i";
$string = 'Learning really complicated regexp';
$found = preg_match($pattern, $string);
if ($found) {
    echo ('The pattern matches the string');
    echo ('');
} else {
    echo ('No match');
}
?>

In this example, the string matches the pattern as it starts by “learn” and ends with “regexp” in a case insensitive regular expression. preg_match will return 1 if the string matches, and 0 if not.

When you provide the optional array, it will contain the results of the matching like so :

  • $ pattern_array[0]= the text that matched the full pattern
  • $ pattern_array[1]=the text matching the first subpattern
  • $ pattern_array[2]=the text matching the second subpattern

…and so on

<?php
echo ('Example 4 ');
$pattern = "/^learn[a-zA-Z0-9\s\-_]*(com|re)[a-zA-Z0-9\s\-_]*regexp$/i";
$string = 'Learning this tutorial with replicated regexp';
$found = preg_match($pattern, $string, $results);
if ($found) {
    echo ('The pattern matches the string');
    echo ('');
    var_dump($results);
} else {
    echo ('No match');
}
?>

It will match, and the array results will contain the whole string first and then the subpattern caught which is “re”.

Flags can be this constant PREG_OFFSET_CAPTURE. When passed, the array returned contains also the appendant string offset or the number of characters before the subpattern match (in the previous example, it would be 0 for the first entry, and 28 for the second one).

You can also pass an int as offset if you want the function to start the search after a certain number of characters.

preg_match_all()

As usual, let’s start with the syntax:

<?php
intpreg_match_all(stringpattern, stringstring, array pattern_array[, intorder]);
?>

This function will do a match on every single occurrence of the pattern you provided and write them in the array. Let’s throw an example for a better understanding!

<?php
echo ('Example 5 ');
$pattern = "/.*/";
$string = '
Learning regex
is as easy
as learning
...
well i don know
';
$found = preg_match_all($pattern, $string, $results);
if ($found) {
    echo ('The pattern matches the string');
    echo ('');
    var_dump($results);
} else {
    echo ('No match');
}
?>

The resulting array will contain an entry for each TEXT. You can alter the way the resulting array will be written by passing the order parameter. You can pass these two different orders:

  • PREG_PATTERN_ORDER: This is the default behaviour as you see if you run the example above. $results[0] will be an array of full patterns match, $results[1] would be an array of text matching the first subpattern, $results[2] an array of text matching the second subpattern and so on….
  • PREG_SET_ORDER: The resulting array will be ordered so that $results[0] will be the first match, $results[1] the second match and so on.

Just give it a try passing different orders in the example above and you will spot the differences.