PHP Regular Expression Functions
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
Thepreg_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 "w3schools" in a string:
$str = "Visit W3Schools";$pattern = "/w3schools/i";echo preg_match($pattern, $str);Try it Yourself »PHP preg_match_all() Function
Thepreg_match_all() function returns how many matches were found for a pattern in astring.
Example
Use a regular expression to do a case-insensitive count of the number ofoccurrences of "ain" in a string:
$str = "The rain in SPAIN falls mainly on the plains.";$pattern = "/ain/i";echo preg_match_all($pattern, $str);Try it Yourself »PHP preg_replace() Function
Thepreg_replace() function replaces all the matches of the pattern in a string withanother string.
Example
Use a case-insensitive regular expression to replace Microsoft withW3Schools in a string:
$str = "Visit Microsoft!";$pattern = "/microsoft/i";echo preg_replace($pattern, "W3Schools", $str);Try it Yourself »PHP preg_split() Function
Thepreg_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);Try it Yourself »PHP preg_grep() Function
Thepreg_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);Try it Yourself »Thepreg_grep() function also has a third parameterPREG_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);Try it Yourself »Grouping
You can use parentheses( ) to apply quantifiers to entire patterns. They also can be usedto select parts of the pattern to be used as a match.
Example
Use grouping to search for the word "banana" by looking forba followed bytwo instances ofna:
$str = "Apples and bananas.";$pattern = "/ba(na){2}/i";echo preg_match($pattern, $str);Try it Yourself »Complete RegExp Reference
For a complete reference, go to ourComplete PHP Regular Expression Reference.
The reference contains descriptions and examples of all Regular Expression functions.

