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.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Below is the syntax:
preg_replace( $pattern1, $replacement1, $subject1, $limit1, $count1 )
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.
Return value for the preg_replace() function: The preg_replace() function will return array only if the subect1 parameter is an array/string.
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.
Below are some examples mentioned:
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:
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:
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:
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:
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.
The above is the detailed content of PHP preg_replace(). For more information, please follow other related articles on the PHP Chinese website!