A regex can be used to look for literal text, text patterns, or a combination of both. To find a match with a PCRE, use the PHP function preg_match(), which normally takes two arguments: a string containing the pattern being searched for and the string to be searched. Although you can pass the pattern directly to the function, it’s often more convenient to store it first in a variable.
PCREs are always enclosed in a pair of delimiters. Traditionally, forward slashes are used (for example, /pattern/), but you can use any nonalphanumeric character other than a backslash.You can also use a pair of curly braces ({}), square brackets ([]), or angle brackets (<>) as delimiters. This can be useful when the pattern includes slashes—for example, when you’re searching for a path name or URL.
Placing an i after the final delimiter, as in the previous email example, makes the search case insensitive.
This is how you use the regex to check whether an email address is likely to be genuine (note that the pattern is enclosed in quotes, as it must be a string):
PCREs are always enclosed in a pair of delimiters. Traditionally, forward slashes are used (for example, /pattern/), but you can use any nonalphanumeric character other than a backslash.You can also use a pair of curly braces ({}), square brackets ([]), or angle brackets (<>) as delimiters. This can be useful when the pattern includes slashes—for example, when you’re searching for a path name or URL.
Placing an i after the final delimiter, as in the previous email example, makes the search case insensitive.
This is how you use the regex to check whether an email address is likely to be genuine (note that the pattern is enclosed in quotes, as it must be a string):
$email = 'david@example.com';
$pattern = '/^\w[-.\w]*@([-a-z0-9]+\.)+[a-z]{2,4}$/i';
$ok = preg_match($pattern, $email);
if (!$ok) {
// code to handle spurious address
}
$pattern = '/^\w[-.\w]*@([-a-z0-9]+\.)+[a-z]{2,4}$/i';
$ok = preg_match($pattern, $email);
if (!$ok) {
// code to handle spurious address
}