PHP Regular Expression Functions
PHP provides a variety of functions that allow you to use regular expressions.
Some common regexp functions are:
preg_match()- Returns 1 if the pattern was found in the string and 0 if notpreg_match_all()- Returns the number of times the pattern was found in the stringpreg_replace()- Returns a new string where the matched patterns is replaced with another stringpreg_split()- Splits a string into an array using matches of a regular expression as separatorspreg_grep()- Returns an array containing only elements from the input that match the given pattern
PHP preg_match() Function
The preg_match() function returns 1 if the pattern was found in the string, and 0 if not.
Example
Use a regular expression to do a case-insensitive search for "example" in a string:
$str = "Visit example.com";
$pattern = "/example/i";
echo preg_match($pattern, $str);
PHP preg_match_all() Function
The preg_match_all() function returns how many matches were found for a pattern in a string.
Example
Use a regular expression to do a case-insensitive count of the number of occurrences of "ain" in a string:
$str = "The rain in SPAIN falls mainly on the plains.";
$pattern = "/ain/i";
echo preg_match_all($pattern, $str);
PHP preg_replace() Function
The preg_replace() function replaces all the matches of the pattern in a string with another string.
Example
Use a case-insensitive regular expression to replace Microsoft with Example in a string:
$str = "Visit Microsoft!";
$pattern = "/microsoft/i";
echo preg_replace($pattern, "Example", $str);
PHP preg_split() Function
The preg_split() function splits a string into an array using matches of a regular expression as separators.
Example
Use preg_split() to split a string into its components:
$str = "This is a text";
$pattern = "/[\s:]/";
$components = preg_split($pattern, $str);
print_r($components);
PHP preg_grep() Function
The preg_grep() function returns an array containing only elements from the input that match the given pattern.
Example
Get items from an array that starts with "p":
$input = [
"Red",
"Pink",
"Green",
"Blue",
"Purple"
];
$result = preg_grep("/^p/i", $input);
print_r($result);
The preg_grep() function also has a third parameter PREG_GREP_INVERT which will invert the result and returns an array containing only elements outside the input that match the given pattern.
Example
Get items from an array that do not starts with "p":
$input = [
"Red",
"Pink",
"Green",
"Blue",
"Purple"
];
$result = preg_grep("/^p/i", $input, PREG_GREP_INVERT);
print_r($result);
Grouping
You can use parentheses ( ) to apply quantifiers to entire patterns. They also can be used to select parts of the pattern to be used as a match.
Example
Use grouping to search for the word "banana" by looking for ba followed by two instances of na:
$str = "Apples and bananas.";
$pattern = "/ba(na){2}/i";
echo preg_match($pattern, $str);
Complete RegExp Reference
For a complete reference, go to the Complete PHP Regular Expression Reference.
The reference contains descriptions and examples of all Regular Expression functions.