Regular expression in php

PHP has historically supported two styles of regular expression syntax: POSIX and Perl. Both types are compiled into PHP by default, but since PHP version 5.3 the POSIX style has been deprecated.

The basics of Regular expression in php

A regular expression is a way of describing a pattern in a piece of text. The exact (or literal) matches you’ve seen so far are a form of regular expression. For example, earlier you searched for regular expression terms such as “shop” and “delivery”.

Matching regular expressions in PHP is more like a strstr() match than an equal comparison because you are matching a string somewhere within another string. (It can be anywhere within that string unless you specify otherwise.)

For example, the string “shop” matches the regular expression “shop”. It also matches the regular expressions “h”, “ho”, and so on

Delimeters

With PCRE regular expressions, each expression must be contained within a pair of delimiters. You may choose any character that is not a letter, a number, a backslash, or whitespace. The delimiter at the start and end of the string must match

The most commonly used delimiter is the forward slash (/). So, for example, if we wanted to write a regular expression to match the word “shop,” we could write

/shop/

If you need to match a literal / inside the regular expression, you will need to escape it with the \ (backslash) character.

/http:\/\//

Table of Character Classes for Use in PCRE-Style Regular Expressions

ClassMatches
[[:alnum:]]Alphanumeric characters
[[:alpha:]]Alphabetic characters
[[:ascii:]]ASCII characters
[[:lower:]]Lowercase letters
[[:upper:]]Uppercase letters
[[:word:]]“Word” characters (letters, digits, or the underscore)
[[:digit:]]Decimal digits
[[:xdigit:]]Hexadecimal digits
[[:punct:]]Punctuation
[[:blank:]]Tabs and spaces
[[:space:]]Whitespace characters
[[:cntrl:]]Control characters
[[:print:]]All printable characters
[[:graph:]]All printable characters except for space
Table of Character Classes for Use in PCRE-Style Regular Expressions

Meta Characters Used in PCRE Regular Expressions Outside Square Brackets

CharacterMeaning
\Escape character
^Match at start of string
$Match at end of string
.Match any character except newline (\n)
|Start of alternative branch (read as OR)
(Start subpattern
)End subpattern
*Repeat zero or more times
+Repeat one or more times
{Start min/max quantifier
}End min/max quantifier
?Mark a subpattern as optional
\Escape character
^NOT, only if used in initial position
~Used to specify character ranges
Meta Characters Used in PCRE Regular Expressions Outside Square Brackets

You can adapt the Smart Form example to use regular expressions by adding the following code to the order processing script:

if (preg_match('/^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/',
$email) === 0) {
 echo "<p>That is not a valid email address.</p>".
"<p>Please return to the previous page and try again.</p>";
 exit;
}
$toaddress = '[email protected]'; // the default value
if (preg_match('/shop|customer service|retail/', $feedback)) {
 $toaddress = '[email protected]';

} else if (preg_match('/deliver|fulfill/', $feedback)) {
 $toaddress = '[email protected]';
} else if (preg_match('/bill|account/', $feedback)) {
 $toaddress = '[email protected]';
}
if (preg_match('/bigcustomer\.com/', $email)) {
 $toaddress = '[email protected]';
}

Leave a Comment