Syntax:
(
string|array $pattern,
string|array $replacement,
string|array $subject,
int $limit = -1,
int &$count = null
)
The first argument: contains the regular expression pattern used in searching for the particular string. The value of this argument can be a string or an array.
The second argument: contains the string used to replace the matched string. The value of this argument can be a string or an array.
The third argument: contains the main string, where the string will be searched and replaced using the pattern. The value of this argument can be a string or an array.
The fourth argument: defines the maximum possible replacements based on each pattern.
The fifth argument: is optional, and it contains the number of replacements done by the pattern.
The function returns: a string or an array if the pattern does one or more searches and replaces/, and the function returns null if no match is found.
preg_replace() Function Examples
The uses of the preg_replace() function have been shown in the next part of the tutorial by using multiple examples.
Example-1: Search and Replace a Word of the String
Create a PHP file with the following script to search the particular string in another string using a pattern and replace the string with another string if any match is found. According to the pattern, the word Java will be searched by ignoring the case of the letter, and if any match is found, it will be replaced by the word PHP. Next, both the original string and the modified string will be printed.
//Define the original string
$original_str = 'Learn JAVA Programming';
//Define the pattern
$pattern = '/Java/i';
//Define the replace string that will be searched and replaced
$replace = 'PHP';
echo "Original string: <b>$original_str</b>";
//Replace the original string based on the pattern and replace string
$replace_str = preg_replace($pattern, $replace, $original_str);
echo "<br>Modified string: <b>$replace_str</b>";
?>
Output:
The following output will appear after executing the above script. The original string contains the word JAVA and the word, PHP, has replaced it.
Example-2: Replace the Digits With the Empty String
Create a PHP file with the following script to search the digits in the string using a pattern and replace the digits with the empty string if any match is found. According to the script, the string contains a digit, 8 that the empty string will replace. Next, both the original string and the modified string will be printed.
$original_str = 'Learn PHP 8 from the basic.';
//Replace the number by empty string of the original string
$replace_str = preg_replace('/\d+/', '', $original_str);
//Print the original string and modified sring
echo "Original string: <b>$original_str</b>";
echo "<br>Modified string: <b>$replace_str</b>";
?>
Output:
The following output will appear after executing the above script.
Example-3: Create an Array After Replacing the Values of Another Array
Create a PHP file with the following script that contains three arrays. The $main_arr array contains string and numeric values where the pattern will be searched. The $pattern array contains patterns used to search the values in the $main_str array. The $replace array contains the replace values used to replace the value of $main_str if any pattern matches. Next, both the original string and the modified string will be printed.
//Declare an array of mixed data
$main_arr = array('Ubuntu', 20, 'Windows', 10, 'Linuxmint', 8);
//Define the search pattern
$pattern = array('/[A-Z]/', '/\d+/');
//Define the corresponding replace string
$replace = array('OS:$0', 'Version:$0');
//Create the new array by modifing array values based on the pattern and replace string
$modified_arr = preg_replace($pattern, $replace, $main_arr);
//Print the original array
echo "<b>The original array:</b> <pre>";
print_r($main_arr);
echo "</pre>";
//Print the modified array
echo "<b>The modified array:</b> <pre>";
print_r($modified_arr);
echo "</pre>";
?>
Output:
The following output will appear after executing the above script.
Example-4: Search and Replace String by Counting the Replacement
Create a PHP file with the following script to count the total number of replacements done by the pattern array and the replace array, which has been defined inside the preg_replace() function. Here, the string ‘programming‘ will be replaced by the strings ‘PHP and’, and the ‘basics’ will be replaced by ‘Python’. Next, both the original string and the modified string will be printed.
//Define the original string
$original_str = 'Learn programming basics from Linuxhint';
//Create the modified string after search and replace and count the total replacement
$modified_str = preg_replace(array('/programming/', '/basics/'),array('PHP and', 'Python'), $original_str, -1, $count);
//Print the original string, modified string and the count value
echo "Original string: <b>$original_str.</b>";
echo "<br>Modified string: <b>$modified_str.</b>";
echo "<br/>Total replacement: <b>$count</b>";
?>
Output:
The following output will appear after executing the above script.
Example-5: Search and Replace the String Based on Limit
Create a PHP file with the following script to show how the number of replacements can be set by using the limit argument of the preg_replace() function. The script’s first preg_replace() function has been used to search and replace the matching word without any limit. The script’s second preg_replace() function has been used to search and replace the matching word with the limit. Next, both the original string and the modified string will be printed.
//Define the original string
$original_str = "Eat to live, not live to eat";
//Print the original string
echo "Original string: <b>$original_str.</b>";
//Search and replace the string without the limit
$modified_str1 = preg_replace("/eat/i", 'EAT', $original_str);
//Search and replace the string based on the limit
$modified_str2 = preg_replace("/eat/i", 'EAT', $original_str, 1);
//Print the modified string
echo "<br>The first Modified string: <b>$modified_str1.</b>";
echo "<br>The second Modified string: <b>$modified_str2.</b>";
?>
Output:
The following output will appear after executing the above script.
Conclusion
Different ways of using the preg_replace() function to search and replace strings have been shown in the examples of this tutorial to help the PHP users to know the use of this function properly.