PHP preg_replace() Function
The PHP preg_replace() function performs a regular expression search and replace.
Syntax
preg_replace(pattern, replacement, subject, limit, count)
Parameters
pattern |
Required. Specify the pattern to search for. It can be either a string or an array with strings. |
replacement |
Required. Specify the string or an array with strings to replace. It replacement is done as per following:
|
subject |
Required. Specify the string or an array with strings in which replacements are being performed. If subject is an array, then the replacements is performed on every element of the array, and returns the replaced array. |
limit |
Optional. Specify the maximum possible replacements for each pattern in each subject string. Default is -1 (no limit). |
count |
Optional. If specified, this variable will contain the number of replacements done. |
Return Value
Returns an array if the subject parameter is an array, or a string otherwise. If matches are found, the new subject will be returned, otherwise subject will be returned unchanged or null if an error occurred.
Example: using backreferences followed by numeric literals
The example below illustrates on using backreferences followed by numeric literals.
<?php $string = 'May 25, 2005'; $pattern = '/(\w+) (\d+), (\d+)/i'; $replacement = '${1}01, $3'; echo preg_replace($pattern, $replacement, $string); ?>
The output of the above code will be:
May01, 2005
Example: using pattern and replacement as arrays
In the example below, pattern and replacement are used as arrays while using this function.
<?php $string = 'The quick brown fox jumps over the lazy dog.'; $pattern = array('/quick/', '/brown/', '/fox/'); $replacement = array('slow', 'black', 'wolf'); echo preg_replace($pattern, $replacement, $string); ?>
The output of the above code will be:
The slow black wolf jumps over the lazy dog.
Example: using count parameter
The example below shows how to use count parameter of this function.
<?php $count = 0; $string = "May 1, 2015"; echo preg_replace(array('/\d/', '/\s/'), '*', $string, -1 , $count)."\n"; echo "Number of replacements: $count"; ?>
The output of the above code will be:
May**,***** Number of replacements: 7
Example: comparing preg_replace() with preg_filter()
The example below illustrates on difference between preg_replace() and preg_filter() functions. preg_filter() only returns the (possibly transformed) subjects where there was a match.
<?php $subject = array('1', 'p', '2', 'q', '3', 'P', 'Q', '4'); $pattern = array('/\d/', '/[a-z]/', '/[1p]/'); $replace = array('P:$0', 'Q:$0', 'R:$0'); echo "preg_replace returns\n"; print_r(preg_replace($pattern, $replace, $subject)); echo "preg_filter returns\n"; print_r(preg_filter($pattern, $replace, $subject)); ?>
The output of the above code will be:
preg_replace returns Array ( [0] => P:R:1 [1] => Q:R:p [2] => P:2 [3] => Q:q [4] => P:3 [5] => P [6] => Q [7] => P:4 ) preg_filter returns Array ( [0] => P:R:1 [1] => Q:R:p [2] => P:2 [3] => Q:q [4] => P:3 [7] => P:4 )
❮ PHP RegEx Reference