Updated May 22, 2023
Introduction to PHP preg_replace()
The PHP preg_replace() function is an inbuilt function in the PHP Programming Language. Preg_replace() function helps in performing the regular expressions for searching and replacing the content. Replacing can be done to a single string or multiple string elements that are inside of the array index values. It is similar to the preg_match() up to some extent but in preg_replace() after performing the pattern match on a string, replacing the string element/elements will be done for the specific text. This is one of the important regular expressions in PHP programming language for manipulating string content. You will know about preg_replace() in brief detail below.
Syntax
Below is the syntax:
preg_replace( $pattern1, $replacement1, $subject1, $limit1, $count1 )
Parameters
The preg_replace() function usually accepts mainly five parameters as mentioned above. They are $pattern1, $replacement1, $subject1, $limit1 and $count1 parameters.
Know about these parameters with brief description below.
- $pattern1: $pattern1 parameter of the preg_replace() function contains string element which is helpful in searching the content. It can either be a single string value or an arrays of string/strings.
- Sreplacement1: $replacement1 parameter of the preg_replace() function is a mandatory parameter. It is helpful to specify the string or a string array with some strings to replace. If this parameter is having a string value then the pattern1 parameter will be an array and all pattern/patterns will be replaced by that specific string. If both the replacement1 and the pattern1 parameters are arrays then every pattern1 will be replaced by the replacement1 counterpart. If the replacement1 is an array and it consists of fewer elements than the pattern1 array then any extra pattern is going to be replaced by the simple empty string.
- $subject1: $subject1 parameter of the preg_replace() function is a string or an array of string that is used to search and replace the string content from the $pattern variable’s string value.
- $limit1: $limit parameter of the preg_replace() function is very important and specifies the possible replacements for every pattern at max.
- $count1: $count parameter of the preg_replace() function is usually an optional parameter. This parameter’s variable’s value-filled with the number which indicates the number of replacements done in the original string.
Return value for the preg_replace() function: The preg_replace() function will return array only if the subect1 parameter is an array/string.
How PHP preg_replace() function work?
Preg_replace() function of PHP programming language works based on the parameters mentioned inside of it. Mainly it works by using the original string which is used when searching and the replacing string/strings. The other parameters may not be that important. Preg_replace() will take regex as it’s a parameter (first parameter) which is used to replace for each match with the 2nd parameter. The second parameter is the plain text whereas the 3rd parameter is the string which is to work. The third parameter can contain $n which is to insert the text matched as a part of the regex rule. If we are writing some complicating multi-part regexes. You can use $0 for using the matched text. Check out the example so that you will know in example 2 below.
Examples to Implement preg_replace() function in PHP
Below are some examples mentioned:
Example #1
In the below example, a variable “$copy_date1” is created with the value “Copyright 1998” and then the same variable is created with the preg_replace() function as a value. So the result of the preg_replace() function will be stored in the “$copy_date1” variable. Inside preg_replace() function, the values 0-9 (all) will be replaced by the string value “2020” and stored in the “$copy_date1” variable. Then the value present in the “$copy_date1” will be printed which is the changed string. Then “Copyright 2020” will be printed in the output.
Code:
<?php
$copy_date1 = "Copyright 1998";
$copy_date1 = preg_replace("([0-9]+)", "2020", $copy_date1);
print $copy_date1;
?>
Output:
Example #2
In the below example, a variable “$a1” is created with the string value “Fool bool tool fool”. Then again the same naming variable is created with the preg_replace() function. In the preg_replace() string value is used to search which are ending with a cool after any alphabet. If there is anything then those string values will be printed one by one after the “Got word:” word. And this result will be stored in the “$a1” variable. Then the word will be printed using the print function.
Code:
<?php
$a1 = "Foo mool bool tool fool";
$a1 = preg_replace("/[A-Za-z]ool\b/", "Got word: $0 \n", $a1);
print $a1;
?>
Output:
Example #3
In the below example, the date will be changed in the string value as mentioned in the replacement1 term. At first a variable “$date1” is created inside of the PHP tags with the value “Sep 29, 2020”. Then the “$pattern1” variable is created with meta-characters that are used to find a word in the character. Then the “$replacement1” variable is created with the value ”${1} 6, $3”. Here “1” inside flower braces will remain the same and then in the 2nd value 6 will be replaced in the place of 29 and $3 represents the 3 string elements inside of the array. If “$1 ” is placed then only Sep will be printed if the $2 is represented then replaced value only 6 will be printed. So the $3 is mentioned to mention all the 3 elements which are to be printed using echo function.
Code:
<?php
$date1 = 'Sep 29, 2020';
$pattern1 = '/(\w+) (\d+), (\d+)/i';
$replacement1 = '${1} 6,$3';
echo preg_replace($pattern1, $replacement1, $date1);
?>
Output:
Example #4
In the below example, multiple string elements are replaced using the array function with the single string values in the index values to change. At first “string1” variable is created with a string sentence with many words. Then “$patterns1” variable is created with array() function as variable. Then inside of the $patterns[index value] is/are stored with a different string that is already present in the original string. Similarly the same is done for the “$replacements1” variable with different string values to change the list of strings inside of the string sentence. Then “$newstr1” variable is created with the value as preg_replace() function. Then echo function is used to print the changed string sentence. Check out this in the output image.
Code:
<?php
$string1 = 'The fast black tiger runs away from the zoo.';
$patterns1 = array();
$patterns1[0] = '/fast/';
$patterns1[1] = '/black/';
$patterns1[2] = '/tiger/';
$replacements1 = array();
$replacements1[2] = 'Slow';
$replacements1[1] = 'Elephant';
$replacements1[0] = 'Jumped and';
$newstr1 = preg_replace($patterns1, $replacements1, $string1);
echo "<b>The String after the replacements:</b> " .$newstr1;
?>
Output:
Conclusion
I hope you understood what is the definition of PHP preg_replace() function with its syntax along with various parameter explanation, How preg_replace() function works along with the various examples to implement preg_replace() function in PHP Programming language.
Recommended Articles
This is a guide to PHP preg_replace(). Here we discuss an introduction, syntax, and working of PHP preg_replace() along with respective examples. You can also go through our other related articles to learn more –