PHP Regular Expressions

Created with Sketch.

PHP Regular Expressions

Summary: in this tutorial, you’ll learn about PHP regular expressions and functions that work with regular expression including preg_match(), preg_match_all(), and preg_replace().

Introduction to the PHP regular expressions

PHP string functions allow you to test if a string contains a substring (str_contains()) or to replace all occurrences of a substring with another string (str_replace()).

However, these functions deal with fixed patterns. They won’t work with flexible patterns. For example, if you want to search any numbers in a string, the str_contains() won’t work.

To search or replace a string using a pattern, you use regular expressions.

A regular expression is a string that describes a pattern such as phone numbers, credit card numbers, and email addresses.

Create regular expressions

To create a regular expression, you place a pattern in forward-slashes like this:

'/pattern/';

Code language: PHP (php)

For example:

<?php

$pattern = '/\d+/';

Code language: PHP (php)

The $pattern is a string. Also, it is a regular expression that matches a number with one or more digits. For example, it matches the numbers 1, 20, 300, etc.

Note that you’ll learn how to form flexible regular expressions in the following tutorial.

The forward-slashes are delimiters. The delimiters can be one of the following characters ~, !, @, #, $ or braces including {}, (), [], <>. The braces help improve regular expressions’ readability in some cases.

Note that you cannot use the alphanumeric, multi-byte, and backslashes (\) as delimiters.

The following regular expression uses the curly braces as delimiters:

<?php

$pattern = '{\d+}';

Code language: PHP (php)

Search strings using regular expressions

To search a string for a match to a pattern, you use the preg_match() and preg_match_all() functions.

PHP preg_match() function

To search based on a regular expression, you use the preg_match() function. For example:

<?php

$pattern = '{\d+}';
$message = 'PHP 8 was released on November 26, 2020';

if (preg_match($pattern, $message)) {
echo "match";
} else {
echo "not match";
}

Code language: PHP (php)

Output:

match

Code language: PHP (php)

The preg_match() searches the $message for a match to the $pattern.

The preg_match() function returns 1 if there is a match in the $message, 0 if it doesn’t, or false on failure.

To get the text that matches the pattern, you add the third parameter to the preg_match() function like the following example:

<?php

$pattern = '{\d+}';
$message = 'PHP 8 was released on November 26, 2020';

if (preg_match($pattern, $message, $matches)) {
print_r($matches);
}

Code language: PHP (php)

Output:

Array
(
[0] => 8
)

Code language: PHP (php)

The $matches parameter contains all the matches. The $matches[0] stores the text that matches the pattern. In this example, it is the number 8.

The $matches[1], $matches[2], … store the texts that match the first, second,… capturing group —more on this in the capturing group tutorial.

The preg_match() only returns the first match and stops searching as soon as it finds the first one. To find all matches, you use the preg_match_all() function.

PHP preg_match_all() function

The preg_match_all() function searches for all matches to a regular expression. For example:

<?php

$pattern = '{\d+}';
$message = 'PHP 8 was released on November 26, 2020';

if (preg_match_all($pattern, $message, $matches)) {
print_r($matches);
}

Code language: PHP (php)

Output:

Array
(
[0] => Array
(
[0] => 8
[1] => 26
[2] => 2020
)
)

Code language: PHP (php)

In this example, the preg_match_all() puts all matches in a multidimensional array with the first element contains the texts (8, 26, and 2020) that match the pattern.

The preg_match_all() function returns the number of matches, which can be zero or a positive number.

Replace strings using regular expressions

To replace strings that match a regular expression, you use the preg_replace() function. For example:

<?php

$pattern = '/\d+/';
$message = 'PHP 8 was released on 11/26/2020';

echo preg_replace($pattern, '%d', $message);

Code language: PHP (php)

Output:

PHP %d was released on %d/%d/%d

 

In this example, the preg_replace() function replaces all numbers in the $message with the string %d.

Summary

  • PHP regular expressions are strings with pattern enclosing in delimiters for example "/pattern/".
  • The preg_match() function searches for a match to a pattern in a string.
  • The preg_match_all() function searches for all matches to a pattern in a string.
  • The preg_replace() function searches a string for matches to a pattern and replaces them with a new string or pattern.

Leave a Reply

Your email address will not be published. Required fields are marked *