Search and Replace Strings With Regular Expressions in PHP

PHP comes with a huge variety of functions to work with strings. You can use these built-in PHP functions to trim strings, join them, or replace them. String functions in PHP are pretty powerful by themselves. You can use them to make case-insensitive changes or do multiple replacements at once. However, they still require you to specify the exact strings that you want to replace.

Sometimes, that’s not enough. You will come across a lot of situations where these replacements and matches have to be based on a pattern instead of some specific string. This is where regular expressions play a very important role.

In this tutorial, you’ll learn how to match, search, and replace strings with regular expressions in PHP.

Let’s begin with finding a match.

Using PHP preg_match() to Search for Patterns in Strings in PHP

The PHP preg_match() function is used to find a match within strings based on your specified pattern. It has two required parameters: the pattern and the string in which we want to find a match.

Keep in mind that this function only returns 1, 0, or false, depending on whether there was a match, no match, or an error occurred. However, you can specify an array as the optional third parameter to fill it up with relevant information about the match.

Names written in the English language usually begin with a capital letter and contain letters from a to z. We use this rule to come up with a very basic pattern and get matches for different names in a given text.

Using preg_match() above returned 1 because it found a match. Remember that this function does not return the total number of matches or the position at which the match was found. It simply returns 1 if the match was found and 0 if no match was found.

The array $names contains a single element which is supposed to be the fully matched string. Any subsequent elements will respective sub-pattern matches. Here is an example:

The preg_match_all() function works just like preg_match() but returns the number of full pattern matches on success or false on failure. One thing you should remember is that the search for the next matches continues from the end position of the previous match.

Observant readers might have noticed that preg_match_all() returned a multi-dimensional array. This is because the first array contains a list of all the full matches, and other arrays are supposed to contain a list of subsequent capture groups if they exist.

As you might have observed, preg_match_all() only returns the number of full pattern matches.

Using preg_replace() to Search and Replace in PHP

You can use the preg_replace() function to search for patterns in a string and then replace them. This function accepts three required parameters. The first one is the pattern you want to find. The second one is the replacement, and the third parameter is the string in which you want to make these replacements. All of these parameters can either be a single value or an array.

Here are some examples of using preg_replace() to help you understand how it works with different kinds of parameters.

Here, the $pattern and $replacement were single string values. Our intent was to replace the phone number with an email address where people can contact us. Using a regular expression ensures that we can replace all different ten-digit phone numbers at once.

This time, we supplied both the patterns and replacements as an array. You can see in the example that the parts matching the first pattern were replaced by the first replacement string in $replacements.

There will be times when you want the matched patterns to be part of the replaced string in PHP. This can be done easily with capture groups. Here is an example in which we add the set of new names to the previous list instead of replacing it.

We used parentheses around our first pattern to turn it into a single capture group. This becomes our first capture group, and using ${1} in the replacement string prepends the old list of names to the new ones.

In our last example, we provided preg_replace() with multiple strings within which we wanted to perform replacements. As you can see, all the replacements are applied to each piece of text individually.

Using preg_replace_callback() to Search and Replace Using Callback Functions in PHP

In the previous section, we saw how powerful regular expressions can be to replace strings which match a specific pattern. We can even use capture groups to reuse the match we found somewhere else in the replacement string.

Sometimes, we need even more control than that to modify the capture group as we see fit before doing any replacements. Using the preg_replace_callback() function will be the perfect choice for that.

We have replaced all the different phone numbers with a single email address in all our examples so far. However, it is more probable that a company with different phone numbers will also have different dedicated email addresses. The preg_replace_callback() function allows us to do these replacements on a case-by-case basis so that we can make appropriate changes to the replacement string based on the contents of the match.

In this example, every team member had a different phone number, and we had to replace the phone number with a combination of phone number and email address. This can be done very easily with preg_replace_callback().

We begin by defining our own function update_email(), which takes in the $matches as its input. The $matches array is the same one we discussed when talking about preg_match(). The first element in $matches will be the whole match, and the other elements will contain the subsequent sub-patterns.

The regular expression in our pattern contains two capture groups to get the name and phone number as separate elements. We use the name of the person to get our email address by converting it to lowercase using strtolower() and then appending @test.com. Finally, we return our new replacement text.

The newer version of our main string now contains both the phone number and the email address of the employees.

Using preg_split() to Split Strings in PHP

The preg_split() function comes in handy when you want to split a main string into multiple substrings based on specific patterns. This is like explode() on steroids.

This function accepts four parameters. The first two are required in every call of the function, and they specify the pattern we should search for and the main string. The next two parameters are optional, and they specify how many strings should be returned and if the returned values should be modified with flags.

Here is an example that shows you how to get individual names from a string containing a big list of names. The names are separated either by one or more spaces, one or more commas, or one or more - or _ characters.

We set the third parameter to -1 to get all the possible splits. The flag PREG_SPLIT_NO_EPMTY is then used to get rid of any empty strings.

Final Thoughts

In this tutorial, we learned how to use some common and useful functions related to regular expressions in PHP. We began by learning how to use preg_match() or preg_match_all() to get all the strings that match a specific pattern. After that, we learned how to use preg_replace() and preg_replace_callback() to replace the matched patterns with some other string.

Regular expressions give us a very powerful and yet fragile tool to manipulate strings in PHP. You have to be careful when defining your patterns, but they save a lot of time if used properly. 

Leave a comment

Your email address will not be published.