PHP preg_replace() Function

What is preg_replace() function?

If you want to search a string using a regular expression pattern and replace the match with your desire content, use preg_replace() function.

With this function, not only a string but, you can search an array of strings. Similarly, the searching regular expression pattern could be a string or an array. The function searches a string (the “subject” parameter) following a regular expression (the “pattern” parameter) and replace the match(s) with another content (the “replacement” parameter).

Syntax:

preg_replace(pattern, replacement, subject, limit, count)

Parameters:

pattern (Mandatory): It can either be a string (Check Example 1) or an array of strings (Check Example 2). This parameter is a regular expression that you’ll search to find matches.

replacement (Mandatory): It can either be a string or an array of string. This parameter is used to replace the matches that are found using the “pattern” parameter. Have a look at what happens if the “pattern” and “replacement” parameters are a string or an array.

  • If this “replacement” parameter is a string and the “pattern” parameter is an array, all the matched strings found by the “pattern” parameter will be replaced by this parameter (Check Example 2).
  • If both “Pattern” parameter and “replacement” parameter are arrays with equal elements, then each matched string found by each “pattern” parameter will be replaced by “replacement” parameter counterpart (Check Example 3).
  • If both “pattern” parameter and “replacement” parameters are arrays but “replacement” array has fewer elements than “pattern” array, then the extra “pattern” will be replaced by an empty string (Check example 4).

subject (Mandatory): This can either be a string or an array. This is the main string where the function searches.

  • If it is an array, the search and replace are performed on every element of the array and the return value is also an array (Check example 5).
  • If it is an associative array, the keys are preserved in the returned value (Check example 6).

limit (Optional): It is a number that tell the maximum replacement number for each “pattern” in each “subject” string. Default number is -1 (which means no limit) (Check example 7).

count (Optional): if you specify this parameter as a variable, the variable stores number of replacement done by the function (Check example 8).

Return Values:

  • It returns the “subject” parameter whether it is modified or not (Check any example).
  • It returns an array if the “subject” parameter is an array (Check example 5), otherwise it returns a string (Check example 1).
  • It returns NULL if an error occurred.

Examples:

Example 1:

<?php
$subject = 'Which function searches and replaces content';
$pattern = '/Which/';
$replacement = 'preg_replace';
$replace_str = preg_replace($pattern, $replacement, $subject);
echo $replace_str;
?>

Output:

preg_replace function searches and replaces content

Example 2:

<?php
$string = 'Both preg_replace and replace_callback are functions.';
$patterns = array('/function/');
$replacements = "built-in function";
echo preg_replace($patterns, $replacements, $string);
?>

Output:

Both preg_replace and replace_callback are built-in functions.

Example 3:

<?php
$string = 'preg_replace function this and that content';
$patterns = array('/this/', '/that/');
$replacements = array('searches', 'replaces');
echo preg_replace($patterns, $replacements, $string);
?>

Output:

preg_replace function searches and replaces content

Example 4:

<?php
$string = 'preg_replace function one, two, and three content';
$patterns = array('/one/', '/two/', '/three/');
$replacements = array('searches', 'replaces');
echo preg_replace($patterns, $replacements, $string);
?>

Output:

preg_replace function searches, replaces, and content

Example 5:

<?php
$string = array('preg_replace function this and replaces content', 'preg_match function this and matches content');
$patterns = array('/this/');
$replacements = array('searches');
$retnrn_str = preg_replace($patterns, $replacements, $string);
for($i=0; $i<count($retnrn_str); $i++){
    echo $retnrn_str[$i]. "<br />";
}
?>

Output:

preg_replace function searches and replaces content
preg_match function searches and matches content

Example 6:

<?php
$string = array("One" => "preg_replace function this and replaces content", "Two" => "preg_match function this and matches content");
$patterns = array('/this/');
$replacements = array('searches');
$retnrn_str = preg_replace($patterns, $replacements, $string);
echo $retnrn_str["One"]. "<br />";
echo $retnrn_str["Two"]. "<br />";
?>

Output:

preg_replace function searches and replaces content
preg_match function searches and matches content

Example 7:

<?php
$subject = "There are three 'the' - the, the the";
$pattern = '/the/';
$replacement = 'one';
$replace_str = preg_replace($pattern, $replacement, $subject, 2);
echo $replace_str;
?>

Output:

There are three ‘one’ – one, the the

Example 8:

<?php
$subject = "There are three 'the' - the, the the";
$pattern = '/the/';
$replacement = 'one';
$replace_str = preg_replace($pattern, $replacement, $subject, -1, $count);
echo $count;
?>

Output:

4

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP preg_replace() Function

preg_replace() function searches a string using a regular expression pattern and replaces the match with your desire content. It is a built-in pcre function in PHP.

Reference:

https://www.php.net/manual/en/function.preg-replace.php