You can use the php preg_replace() function to perform a regular expression (regex) search on a string or array of strings, and returns a string or an array of strings where all matches of the regex pattern or list of regex patterns found are replaced with substrings.
$old_string = "This is a string with too much whitespace.";
$pattern = '/ss+/';
$new_string = preg_replace($pattern," ",$old_string);
echo $new_string; // Output: "This is a string with too much whitespace."
From the php documentation, the php preg_replace() function takes 5 parameters:
preg_replace(
string|array $pattern,
string|array $replacement,
string|array $subject,
int $limit = -1,
int &$count = null
)
The first parameter is the pattern (or array of patterns) we want to search for. The second parameter is what we will replace the first parameter with. The third parameter is the string (or array of strings) we will be searching.
The fourth parameter is the maximum number of replacements you want to perform, and you can also pass a fifth parameter which will return the number of times a replacement occurred.
If you don’t need to use regex expressions to perform string replacement, you can use php str_replace() instead.
Using preg_replace() to Replace a Pattern with php
We can use the php preg_replace() function to replace a letters or words in a string based on a pattern very easily in our php code.
Let’s say I have the following string:
$sample_string = "This is a string with some text that we will search and replace with a regex expression.";
Let’s say I want to replace all of the 1 and 2 letter words with a space. We can easily do that with the following call to preg_replace():
$sample_string = "This is a string with some text that we will search and replace with a regex expression.";
$pattern = '/(?
String Replacement with Multiple Patterns Using preg_replace()
One of the cool things with the php preg_replace() function is you can replace multiple patterns in a string by passing in an array of different patterns to the function.
Let's say I have the same string from above.
$sample_string= "This is a string with some text that we will search and replace with a regex expression.";
Let's replace all of the one letter words with "one" and all of the two letter words with the word "two". We can do this by passing an array of patterns and array of replacements to preg_replace().
$sample_string = "This is a string with some text that we will search and replace with a regex expression.";
$patterns = ['/(?
There are a few things to note here.
First, if the first parameter is an array and the second parameter is a string, the replacement string will be used for all replacements.
$sample_string = "This is a string with some text that we will search and replace with a regex expression.";
$patterns = ['/(?
Second, if the first parameter is an array and the second parameter is an array with length shorter than the first parameter, an empty string will be used for the remaining replacements.
$sample_string = "This is a string with some text that we will search and replace with a regex expression.";
$patterns = ['/(?
Replacing Strings in an Array with preg_replace()
We can also replace strings in an array of strings with the php preg_replace() function.
Let's say I have the following array of strings:
$array_of_strings = array("This","is","an","array","of","strings");
Let's replace all of the one letter words with "one". Let's also replace all of the two letter words with the word "two". We can do this by passing an array of patterns and array of replacements to preg_replace() and preg_replace() will make the appropriate replacements on each element of the array.
$array_of_strings = array("This","is","an","array","of","strings");
$patterns = ['/(?
Hopefully this article has been helpful for you to understand how you can use the php preg_replace() function to replace strings with other strings based on regex patterns in your php code.