PHP preg_replace

Created with Sketch.

PHP preg_replace

Summary: in this tutorial, you’ll learn how to use the PHP preg_replace() function to search and replace using regular expressions.

Introduction to the PHP preg_replace() function

The preg_replace() function searches for matches and replaces them with a pattern.

The following shows how to use the preg_replace() function with a single string:

preg_replace(
string $pattern,
string $replacement,
string $subject,
int $limit = -1,
int &$count = null
): string

Code language: PHP (php)

The preg_replace() function has the following parameters:

  • $pattern is a regular expression to match.
  • $replacement is a string to replace. It may contain the backreferences.
  • $subject is a string to search and replace.
  • $limit is the maximum possible replacement for the pattern. By default, it is -1, which is unlimited.
  • $count stores the number of replacements.

If the $subject matches the $pattern, the preg_replace() function replaces the match with the $replacement and returns it.

PHP preg_replace() function examples

Let’s take some examples of using the preg_replace() function.

1) Using the PHP preg_replace() function to change the date format

The following example uses the preg_replace() function to change the date string from dd-mm-yyyy to mm-dd-yyyy format:

<?php

$pattern = '/(\d{2})-(\d{2})-(\d{4})/';
$replacement = '\2-\1-\3';

$str = '25-12-2021';

echo preg_replace($pattern, $replacement, $str);

Code language: PHP (php)

Output:

12-25-2021

Code language: PHP (php)

How it works.

The following regular expression matches the date in the dd-mm-yyyy format:

/(\d{2})-(\d{2})-(\d{4})/

Code language: PHP (php)

It consists of three capturing groups for day (\d{2}), month (\d{2}), and year (\d{4}).

The replacement string contains three backreferences \2 for the second capturing group which is month, \1 for the first capturing group which is day, and \3 for the third capturing group which is the year.

Therefore, the preg_replace() changes the format of the date from dd-mm-yyyy to mm-dd-yyyy.

2) Using the PHP preg_replace() function to remove the excessive whitespace

The following example uses the preg_replace() function to strip excess whitespace from a string:

<?php

$str = 'PHP is awesome';
echo preg_replace('/\s\s+/', ' ', $str);

Code language: PHP (php)

Output:

PHP is awesome

Code language: PHP (php)

The pattern /\s\s+/ matches one or more spaces.

PHP preg_replace() function with arrays

The preg_replace() function also accepts the $pattern, $replacement, and $subject as arrays:

preg_replace(
string|array $pattern,
string|array $replacement,
string|array $subject,
int $limit = -1,
int &$count = null
): string|array|null

Code language: PHP (php)

If both $pattern and $replacement are arrays and $subject is a string, the preg_replace() function will replace each pattern in the $pattern array with the replacement in the $replacement array.

If the $replacement array has fewer elements than the $pattern array, the preg_replace() function wil replace extra patterns with an empty string.

If the $subject is an array, the preg_replace() function searches and replaces each string in the array and returns the result as an array.

Summary

  • Use the PHP preg_replace() function to search and replace strings using regular expressions.

Leave a Reply

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