📖 PHP Regular Expressions
A Regular Expression is a text string that defines a character pattern. It is used to validate form input to insure it matches a certain pattern. Commonly used for phone numbers, zip codes, etc.
preg_match()
- PHP function used to search a string.
- Returns a 1 if a match is found.
- If the pattern is not found a 0 will be returned.
Format
preg_match(/search_pattern/, $your_string);
// where search_pattern needs to be a regular expression and $your_string is the string to test.
Creating a search pattern.
Search patterns are delineated by slashes. /search_pattern/
Some or all of the following can be used to create a search pattern.
Regular Expression Flags
| |
---|---|
i
|
Case Insensitive |
g
|
Global (all matches) |
Regular Expression Character Positions
| |
^
|
beginning of the text string |
$
|
end of the text string |
\b
|
presence of a word boundary |
\B
|
absence of a word boundary |
Regular Expression Character Classes
| |
\d
|
digit (0-9) |
\D
|
non-digit |
\w
|
word character (a-z, A-Z, 0-9, _) |
\W
|
non-word character (special characters) |
\s
|
white-space character (blank space, tab, newline, carriage return, or formfeed |
\S
|
non-white spce character |
.
|
any character |
[chars]
|
match any character in the list of characters chars |
[^chars]
|
match any character not in the list of characters chars |
[char1-charN]
|
match any character in the range char1 - charN |
[^char1-charN]
|
match any character not in the range char1 - charN |
[a-z]
|
match lowercase letters |
[A-Z]
|
match uppercase letters |
[a-zA-Z]
|
match letters (uppercase or lowercase) |
[0-9]
|
match digits |
Repetition Characters
| |
*
|
Repeat 0 or more times |
?
|
Repeat 0 or 1 time |
+
|
Repeat 1 or more times |
{n}
|
Repeat exactly n times |
{n,}
|
Repeat at least n times |
{n,m}
|
Repeat at least n times but no more than m times |
Escape Sequences
| |
\/
|
/ |
\\
|
\ |
\.
|
. |
\*
|
* |
\+
|
+ |
\?
|
? |
\|
|
| |
\(\)
|
() |
\{\}
|
{} |
\^
|
^ |
\$
|
$ |
\n
|
new line |
\r
|
carriage return |
\t
|
tab |
Example
if (empty($_POST['email'])){
// check for missing email
$emailError = '<span class="text-danger">Required</span>';
$valid = false;
} else {
$email = trim($_POST['email']);
// validate email using a regular expression
if (!preg_match('/[-\w.]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}/', $email)) {
// returns 1 (true) for match, 0 (false) for no match
$emailError = "<p class='error'>Invalid email address</p>";
$valid = false;
}
}
Above is an example for checking an email from a form with a regular expression to determine if it matches the pattern of a valid email address. This does not verify an actual working email, only that it matches the pattern (user@domain.tld) of a valid email address. You can try out more regular expressions with the regular expression tester.
U.S. Zip Code | \d{5}(-\d{4})? |
---|---|
U.S. Phone Number | \(?(\d{3})\)?[ -.](\d{3})[ -.](\d{4}) |
Email address | [-\w.]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4} |
Date | ([01]?\d)[-\/ .]([123]?\d)[-\/ .](\d{4}) |
Web address | ((\bhttps?:\/\/)|(\bwww\.))\S* |