PHP | preg_replace() Function Last Updated : 31 May, 2023 Comments Improve Suggest changes Like Article Like Report The preg_replace() function is an inbuilt function in PHP that is used to perform a regular expression for search and replace the content. Syntax: preg_replace( $pattern, $replacement, $subject, $limit, $count ) Parameters: This function accepts five parameters as mentioned above and described below. $pattern: This parameter contains the string element which is used to search the content and it can be a string or array of strings.$replacement: It is a mandatory parameter that specifies the string or an array with strings to replace.$subject: The string or an array with strings to search and replace.$limit: This parameter specifies the maximum possible replacements for each pattern.$count: It is an optional parameter. This variable will be filled with the number of replacements done. Return Value: This function returns an array if the subject parameter is an array, or a string otherwise. The below programs illustrate the preg_replace() function in PHP. Program 1: The following program replaces string by using the regular expression parameter. php <?php // PHP program to illustrate // preg_replace function $string = 'November 01, 2018'; $pattern = '/(\w+) (\d+), (\d+)/i'; $replacement = '${1} 02, $3'; // print output of function echo preg_replace($pattern, $replacement, $string); ?> Output:November 02, 2018 Program 2 : php <?php // PHP program to illustrate // preg_replace function $subject = array('1', 'GFG', '2', 'Geeks', '3', 'GCET', 'Contribute', '4'); $pattern = array('/\d/', '/[a-z]/', '/[1a]/'); $replace = array('X:$0', 'Y:$0', 'Z:$0'); // Print Result return by function echo "preg_replace returns\n"; print_r(preg_replace($pattern, $replace, $subject)); ?> Output:preg_replace returns Array ( [0] => X:Z:1 [1] => GFG [2] => X:2 [3] => GY:eY:eY:kY:s [4] => X:3 [5] => GCET [6] => CY:oY:nY:tY:rY:iY:bY:uY:tY:e [7] => X:4 ) Program 3: php <?php // PHP program to illustrate // preg_replace function $count = 0; // Display result after replace and count echo preg_replace(array('/\d/', '/\s/'), '*', 'Geeks 4 Geeks', -1, $count); echo "\n" . $count; ?> Output:Geeks***Geeks 3 Reference: http://php.net/manual/en/function.preg-replace.php Comment More infoAdvertise with us Next Article PHP | preg_replace() Function R R_Raj Follow Improve Article Tags : Web Technologies PHP PHP-function Similar Reads PHP | preg_split() Function The preg_split() function is an inbuilt function in PHP which is used to convert the given string into an array. The function splits the string into smaller strings or sub-strings of length which is specified by the user. If the limit is specified then small string or sub-strings up to limit return 3 min read PHP str_replace() Function In this article, we will see how to replace the occurrence of the search string with the replacing string using the str_replace() function in PHP, along with understanding their implementation through the examples. The str_replace() is a built-in function in PHP and is used to replace all the occurr 3 min read PHP | ereg_replace() Function The ereg_replace() is an inbuilt function in PHP and is used to search a string pattern in an other string. If pattern is found in the original string then it will replace matching text with a replacement string. You may refer to the article on Regular Expression for basic understanding of pattern m 3 min read PHP array_replace() Function The array_replace() function is a builtin function in PHP and it takes a list of arrays separated by commas (,) as parameters and replaces all those values of the first array that have same keys in the other arrays. The replacement is done according to the following rules: If a key in the first arra 3 min read PHP substr_replace() Function The substr_replace() function is a built-in function in PHP and is used to replace a part of a string with another string. The index in the original string from which the replacement is to be performed needs to be passed as a parameter. If desired, the length up to which the replacement is to be don 3 min read Like