What is a Regular Expression?
A regular expression describes what you are looking for in a text. You write a pattern made of characters, and the search uses that pattern to find matches.
Patterns can be as small as one character or far more elaborate.
Once you have a pattern, you can use it for any kind of text searching or replacing.
Syntax
PHP writes a regular expression as a string with up to three parts: delimiters, the pattern itself and, if you need them, modifiers.
Regular Expression Syntax
Here the slashes are the delimiters, PromptCookbook is the pattern and i is a modifier:
$exp = "/PromptCookbook/i";
Regular Expression Functions
Several PHP functions work with regular expressions. These three are the ones you will use most:
preg_match()- Gives 1 if the string contains the pattern, otherwise 0preg_match_all()- Counts how many times the pattern occurs in the string; the count can be 0preg_replace()- Gives back a new string with the matched parts swapped for another string
Using preg_match()
To find out whether a string contains a match for a pattern, call preg_match().
preg_match() Example
Search a string for "PromptCookbook" while ignoring case. The result is 1 because a match exists:
Output: PHP script
<?php
$str = "Visit PromptCookbook";
$pattern = "/PromptCookbook/i";
echo preg_match($pattern, $str);
?>
Using preg_match_all()
When you need the number of matches for a pattern in a string, use preg_match_all().
preg_match_all() Example
Count how many times "ain" appears in a string, ignoring case:
Output: PHP script
<?php
$str = "The rain in Spain falls mainly on the plain";
$pattern = "/ain/i";
echo preg_match_all($pattern, $str);
?>
Using preg_replace()
With preg_replace(), every match of the pattern in a string gets swapped for another string.
preg_replace() Example
Replace Microsoft with PromptCookbook in a string, using a pattern that ignores case:
Output: PHP script
<?php
$str = "Visit Microsoft!";
$pattern = "/microsoft/i";
echo preg_replace($pattern, "PromptCookbook", $str);
?>
Regular Expression Modifiers
Adding a modifier changes the way a search behaves.
i- Ignores the difference between uppercase and lowercasem- Turns on multiline mode, so patterns for the start or end of a string match at the start or end of every lineu- Makes patterns encoded in UTF-8 match correctly
Regular Expression Patterns
Square brackets let you match one character out of a set or range:
[abc]- One character, chosen from those listed in the brackets[^abc]- One character that is not listed in the brackets[a-z]- One lowercase letter[A-z]- One letter, uppercase or lowercase; this range also covers the special characters that sit between Z and a[A-Z]- One uppercase letter[123]- One digit, chosen from those listed in the brackets[0-5]- One digit from 0 through 5[0-9]- One digit from 0 through 9
Metacharacters
Some characters are not taken literally in a pattern. These metacharacters each have their own job:
|- Separates alternatives, and any one of them can match, as in cat|dog|fish.- Exactly one character of any kind^- Ties the match to the start of the string, as in ^Hello$- Ties the match to the end of the string, as in World$\d- A digit\s- A whitespace character\b- A word edge: \bWORD matches at the start of a word, and WORD\b matches at the end of one\x{xxxx}- The Unicode character whose hexadecimal number is xxxx (needs theumodifier)
Quantifiers
A quantifier says how many times something may occur:
n+- A string with one or more n'sn*- A string where n appears any number of times, including nonen?- A string where n appears once or not at alln{x}- A string with a run of exactly X n'sn{x,y}- A string with a run of X to Y n'sn{x,}- A string with a run of X or more n's
Grouping
Wrap part of a pattern in parentheses () and a quantifier will apply to that whole part. Parentheses also let you pick out parts of the pattern to use as a match.
Grouping Example
Find the word "banana" by searching for ba plus two copies of na:
Output: PHP script
<?php
$str = "I like bananas";
$pattern = "/ba(na){2}/i";
echo preg_match($pattern, $str);
?>