Using Regular Expressions in PHP

If you need a refresher on how Regular Expressions work, check out our Interactive Tutorial first!

PHP supports regular expressions through the use of the PCRE (Perl Compatible Regular Expressions) library which is enabled in almost all PHP installations. When calling any of the methods below, PHP requires that each pattern starts and ends with the same delimiter to differentiate it from a normal string, with the most common delimiter being the forward slash character.

For example, you would write the regular expression "\w+" as "/\w+/" in PHP to represent an expression that matches one or more alphanumeric characters.

Matching a string

In PHP, you can use the preg_match() function to test whether a regular expression matches a specific string. Note that this function stops after the first match, so this is best suited for testing a regular expression more than extracting data.

$has_matches = preg_match($pattern, $input_str, $matches_out)
// Lets use a regular expression to match a date string. Ignore // the output since we are just testing if the regex matches. $regex = "/[a-zA-Z]+ \d+/"; if (preg_match($regex, "June 24")) { // Indeed, the expression "[a-zA-Z]+ \d+" matches the date string echo "Found a match!"; } else { // If preg_match() returns false, then the regex does not // match the string echo "The regex pattern does not match. :("; }

Capturing groups

Unlike the preg_match() function above, we can use preg_match_all() function to perform a global search of the input string. If preg_match_all() returns true, then you can iterate over the array specified by $matches_out.

$has_matches = preg_match_all($pattern, $input_str, $matches_out)
// Lets write a regular expression to extract the day of month in // a string with numerous dates $pattern = "/[a-zA-Z]+ (\d+)/"; $input_str = "June 24, August 13, and December 30"; if (preg_match_all($pattern, $input_str, $matches_out)) { // $matches_out is now an Array of size equal to N+1, for N // number of groups you are capturing in your regex, and +1 // for the substrings that matched. // This will print "2" because we are capturing only one group echo count($matches_out); // In addition, each value in $matches_out is another Array of // size M, for M number of matches of the regex in the input. // This will print "3" for the three dates in our input string echo count($matches_out[0]); // $matches_out[0] is an Array of the matched strings from the // input string. // This prints an Array ("June 24", "August 13", "December 30") print_r($matches_out[0]); // $matches_out[1], $matches_out[2], etc. are Arrays filled with // the captured data in the same order as in the regex pattern. // This prints an Array ("24", "13", "30") print_r($matches_out[1]); }

Finding and replacing strings

It is handy sometimes to find and replace a string using regular expressions, for example, to replace all instances of an old email domain with a new one, or to swap the order of some text. You can do this with the preg_replace() function.

$replaced_str = preg_replace($pattern, $partial_replacement_str, $input_str)
// Lets try and reverse the order of the day and month in a date // string. Notice how we can reference the two groups directly // by index (ie. $1 and $2). $regex = "/([a-zA-Z]+) (\d+)/"; $new_string = preg_replace($regex, "$2 of $1", "June 24"); // The string returned is either the same input string if the // regex does not match, or the transformed string. // This prints "24 of June" echo $new_string;


For more information about using regular expressions in PHP, please visit the following links: