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:
preg_match ( string $pattern , string $subject , array &$matches = null , int $flags = 0 , int $offset = 0 );
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
<?php preg_match('/tutsplus/i', 'The code.tutsplus.com is one of my favorite websites among the group of all tutsplus websites!', $matches); /** $matches value: Array ( [0] => tutsplus ) **/
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
<?php preg_match('/code.(tutsplus).com/i', 'The code.tutsplus.com is one of my favorite websites among the group of all tutsplus websites!', $matches); /** $matches value: Array ( [0] => code.tutsplus.com [1] => tutsplus ) **/
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
<?php preg_match('/< *img[^>]*src *= *["']?([^"']*)/i', '<img src="https://example.com/image1.jpg" /><img src="http://example.com/image2.jpg" />', $matches); /** $matches value: Array ( [0] => <img src="http://example.com/image1.jpg [1] => http://example.com/image1.jpg ) **/
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
<?php preg_match('#<div class="tutsplus">(. *?)</div>#s', '<div>Some text.</div><div class="tutsplus">Special text.</div><div>Some more text.</div>', $matches); /** $matches value: Array ( [0] => <div class="tutsplus">Special text.</div> [1] => Special text. ) **/
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
<?php preg_match_all('/tutsplus/i', 'The code.tutsplus.com is one of my favorite websites among the group of all tutsplus websites!', $matches); /** $matches value: Array ( [0] => Array ( [0] => tutsplus [1] => tutsplus ) ) **/
Example 2: Simple Pattern Matching
<?php preg_match_all('/code.(tutsplus).com/i', 'The code.tutsplus.com is one of my favorite websites among the group of all tutsplus websites!', $matches); /** $matches value: Array ( [0] => Array ( [0] => code.tutsplus.com ) [1] => Array ( [0] => tutsplus ) ) **/
Example 3: Find Image Paths
<?php preg_match_all('/< *img[^>]*src *= *["']?([^"']*)/i', '<img src="http://example.com/image1.jpg" /><img src="http://example.com/image2.jpg" />', $matches); /** $matches value: Array ( [0] => Array ( [0] => <img src="http://example.com/image1.jpg [1] => <img src="http://example.com/image2.jpg ) [1] => Array ( [0] => http://example.com/image1.jpg [1] => http://example.com/image2.jpg ) ) **/
Example 4: Find Specific HTML Tags
<?php preg_match_all('#<div class="tutsplus">(.*?)</div>#s', '<div>Some text.</div><div class="tutsplus">Special text.</div><div>Some more text.</div><div class="tutsplus">More special text.</div>', $matches); /** $matches value: Array ( [0] => Array ( [0] => <div class="tutsplus">Special text.</div> [1] => <div class="tutsplus">More special text.</div> ) [1] => Array ( [0] => Special text. [1] => More special text. ) ) **/
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:
preg_replace ( string|array $pattern , string|array $replacement , string|array $subject , int $limit = -1 , int &$count = null );
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
<?php $sourceString = "Let's fix the spelling of Tutplus in this sentence. The Tutplus is a great resource for online learning!"; echo preg_replace('/Tutplus/i', 'tutsplus', $sourceString); /** OUTPUT: Let's fix the spelling of tutsplus in this sentence. The tutsplus is a great resource for online learning! **/
In this example, we’re just replacing a single word wherever it occurs in the text.
Example 2: Replacement With an Array
<?php $sourceString = "This text contains shortcodes like {LOGO}, {HOMEPAGE_URL} and {CONTACT_EMAIL}. It should be replaced with the actual content before it's rendered."; echo preg_replace( array('/{LOGO}/i', '/{HOMEPAGE_URL}/i', '/{CONTACT_EMAIL}/i'), array('<img src="http://example.com/logo.png"', 'http://example.com', '[email protected]'), $sourceString ); /** OUTPUT: This text contains shortcodes like <img src="http://example.com/logo.png", http://example.com and [email protected]. It should be replaced with the actual content before it's rendered. **/
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
<?php $sourceString = '<p>Some text here.</p><p><img src="http://code.tutsplus.com/images/logo.jpg" /></p><p><img src="images/logo2.jpg" /></p><p>More text here.</p>'; $pattern = '/<img(s+)src="images//i'; $replace = '<img src="http://example.com/images/'; echo preg_replace($pattern, $replace, $sourceString); /** OUTPUT: <p>Some text here.</p><p><img src="http://example.com/images/logo.jpg" /></p><p><img src="http://example.com/images/logo2.jpg" /></p><p>More text here.</p> **/
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
<?php $sourceString = '<div>Some text.</div><div class="tutsplus">Special text.</div><div>Some more text.</div>'; $pattern = '#<div class="tutsplus">(.*?)</div>#s'; $replace = '<b>$1</b>'; echo preg_replace($pattern, $replace, $sourceString); /** OUTPUT: <div>Some text.</div><b>Special text.</b><div>Some more text.</div> **/
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:
preg_grep ( string $pattern , array $array , int $flags = 0 );
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.
<?php $arrWebsites = [ "code.tutsplus.com", "example.com", "someotherdomain.com", "webdesign.tutsplus.com", "business.tutsplus.com" ]; $result = preg_grep("/(.*)[.]tutsplus[.](.*)/i", $arrWebsites); print_r($result); /** OUTPUT: Array ( [0] => code.tutsplus.com [3] => webdesign.tutsplus.com [4] => business.tutsplus.com ) **/ ?>
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.
<?php $sourceString = '<div>Some text.</div><div class="tutsplus">Special text.</div><div>Some more text.</div>'; $pattern = '#<div class="tutsplus">(.*?)</div>#s'; echo preg_replace_callback($pattern, function ($matches) {return '<b>' . $matches[1] . '</b>';}, $sourceString); /** OUTPUT: <div>Some text.</div><b>Special text.</b><div>Some more text.</div> **/ ?>
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.
preg_split ( string $pattern , string $subject , int $limit = -1 , int $flags = 0 );
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.
<?php $sourceString = "This is a long text. It contains a lot of line breaks. Let's split it with the preg_split function."; $result = preg_split ("/\n/", $sourceString); print_r($result); /** OUTPUT: Array ( [0] => This is a long text. [1] => It contains a lot of line breaks. [2] => Let's split it with the preg_split function. ) **/ ?>
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.