PHP | preg_replace() Function

The preg_replace() function is an inbuilt function in PHP which 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 mention above and describe below.

  • $pattern: This parameter contains the string element which is used to search the content and it can be a string or array of string.
  • $replacement: It is mandatory parameter which 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 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.

Below programs illustrate the preg_replace() function in PHP:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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);
?>

chevron_right


Output:

November 02, 2018

Program 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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 Rsult return by function
echo "preg_replace returns\n";
print_r(preg_replace($pattern, $replace, $subject)); 
?>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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;
?>

chevron_right


Output:

Geeks***Geeks
3

Reference: http://php.net/manual/en/function.preg-replace.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.