How to Work With Regular Expressions in PHP

In this article, we’ll go through the PHP functions that allow you to use regular expressions to perform search and replace operations on strings.

What Is a Regular Expression?

Regular expressions—also called “regex”—are search patterns for text. These patterns can be processed by a regex engine to find substrings, validate that a string matches a pattern, or search and replace text.

Regular expressions are very powerful, allowing you to perform search and replace operations on strings effortlessly. They are widely used for pattern matching and validation in different languages. Today, we’ll discuss regex in the context of PHP.

In this post, we’ll go through the different functions available in PHP that deal with regular expressions. Specifically, we’ll explore the preg_* family functions along with real-world examples.

Regular Expression Functions in PHP

In this section, we’ll go through a variety of regular expression functions in PHP with real-world examples.

The preg_match Function

The preg_match function performs a regular expression match against a string. Let’s take a look at the syntax:

The first argument is the pattern which you want to search for. You should provide it in a regular expression format. The second argument is the subject string against which the regular expression match is performed. Next, the third argument is optional, but if it’s provided, it would be filled with search results. In most cases, you would need these three arguments.

The fourth argument is a combination of PREG_OFFSET_CAPTURE and PREG_UNMATCHED_AS_NULL flags that you could use to fetch some extra information about the search results.

Finally, the fifth argument is the offset, which allows you to specify the position where the search will start. So if you don’t want to start searching from the beginning of the string, this is the argument you should use.

So that was the syntax of the preg_match function. Let’s have a look at a couple of real-world examples.

Example 1: Simple String Matching

In this first example, we’re checking whether the source text contains a specific word. Since the tutsplus string exists in the source text, the $matches variable is populated with the matched string.

Example 2: Simple Pattern Matching

In this example, we’ve used pattern matching. As it’s pattern matching, the $matches[0] element contains the text which matched the full pattern, and the $matches[1] element contains the text which matched the first parenthesized pattern.

Example 3: Find Image Paths

Next, we’re trying to find the value of the src attribute in the first <img> tag. It’s important to note that it returns only the first match, even though there are multiple matches.

Example 4: Find Specific HTML Tags

Finally, here’s an example which demonstrates how to extract text from within the specific HTML tags.

The preg_match_all Function

The preg_match_all function is very similar to the preg_match function, with the exception that it performs a global regular expression match. And thus, it returns all the values that are matched against the pattern.

Also, the syntax of the preg_match_all function is the same as that of the preg_match function.

Let’s quickly go through a couple of examples to see how it works. In fact, we’ll revise the examples that we’ve discussed in the previous section with the preg_match_all function instead of the preg_match function. In this way, it should help you to understand the difference between these two variants.

Example 1: Simple String Matching

Example 2: Simple Pattern Matching

Example 3: Find Image Paths

Example 4: Find Specific HTML Tags

It’s important to notice that when you use the preg_match_all function, the $matches variable is initialized with a multidimensional array. The $matches[0] element is initialized with an array of all the values that are matched against the full pattern. On the other hand, the $matches[1] element is initialized with an array of values that are matched against the first parenthesized sub-pattern, the $matches[2] element contains an array of values that are matched against the second parenthesized sub-pattern, and so on.

The preg_match_all function is a very powerful function, and you will come across it often in your day-to-day PHP development.

The preg_replace Function

The preg_replace function performs a regular expression search and allows you to replace the search results with other strings. Let’s look at its format:

The first argument is the pattern you want to search for. It can be either a string or an array of strings. In the second argument, you can pass a string or an array of strings to replace it with. It’s important to note that if you provide the $replacement parameter as a string and the $pattern parameter contains an array, all patterns that are matched are replaced with the $replacement string. On the other hand, if you provide both $pattern and $replacement as an array, it’ll be a one-to-one search and replace operation.

The third argument is a string or an array of strings to search and replace. Moving further, the fourth argument allows you to specify the maximum number of replacements you want to perform for each pattern. Finally, the last argument allows you to pass a variable which will be filled with the number of replacements done.

Let’s have a look at a couple of examples now.

Example 1: Simple String Replacement

In this example, we’re just replacing a single word wherever it occurs in the text.

Example 2: Replacement With an Array

In this example, we have a list of patterns to match and a list of content to replace each one with. This is akin to how shortcodes work in WordPress.

Example 3: Convert Relative URLs to Absolute URLs

In this example, we’re replacing relative image URLs with absolute URLs. Note that this is a bit simplistic—it’s not supposed to be a real-world demonstration of how to rewrite relative URLs as absolute URLs.

Example 4: Replacement With References

In this last example, we’ve used the $1 reference in the $replace variable, which references the text captured by the first parenthesized pattern. In this way, you could also use the captured text itself in the replace string.

While we’re discussing the preg_replace function, you should also know that there’s another function, preg_filter, which works the same as the preg_replace function, except that it only returns subjects that are matched against the pattern.

The preg_grep Function

The preg_grep function allows you to perform pattern matching to an array of values.

The syntax of the preg_grep function looks like this:

The first argument is the pattern which you want to search for, and the second argument is the input array.

The preg_grep function is really useful when you have an array of items, and you want to extract items with a specific pattern.

Let’s quickly go through the following example.

In the above example, we’ve used the preg_grep function to filter websites that belong to the tutsplus group.

The preg_replace_callback Function

The preg_replace_callback function is almost identical to the preg_replace function, except that you need to specify a callback function instead of the replacement string in the second argument.

Let’s convert one of the preg_replace examples which we’ve discussed earlier with the preg_replace_callback counterpart.

As you can see, we’ve provided an anonymous function in the second argument of the preg_replace_callback function. It would receive the captured text elements in the $matches array, and you can use it for replacement.

The preg_split Function

The preg_split function allows you to split a string by a regular expression.

Let’s have a look at the syntax of the preg_split function.

The first argument is the pattern which you want to search for, and the second argument is the input string.

Let’s have a look at the following example to understand how it works.

Here you can see we use a pattern that matches line breaks, and the output returns the individual lines of the text. 

Conclusion

Today, we discussed regular expression functions in PHP. We explored a variety of preg_* family functions, along with real-world examples to understand how they work.