PHP uses two types of functions that will work with regular expressions. One type of function is the preg set of functions, which include preg_match and preg_match_all these functions work with the PCRE Library.

The second type is an old form of regular expression which is compatible with POSIX called ereg and eregi which is now depreciated since PHP 5.3.

For this tutorial all the examples will make use of either the preg_match or preg_match_all and PCRE compatible regex patterns.

Matching a Valid Email Address

$email = 'editor@thedaily.com'
if (!preg_match('^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$^', $email)) {
echo 'The email address you entered was invalid.';
}

PHP

Matching a string between 4 and 128 characters

$string = "tutorial";
if (!preg_match('#^(.){4,128}$#', $string)) {
 echo 'The string you entered is invalid.';
}

PHP

Matching an integer between 1 and 16 characters in length

$int = 4;
if (!preg_match('#^[0-9]{1,16}$#', $int)) {
echo 'That is not a valid integer.';
}

PHP

Matching Content between any given HTML tag with attributes

$html = ' <style class="test">this is the matched pattern </style>';
 
preg_match('/(.*)?<\/style>/', $html, $match);
 
print $match[2];


PHP

Matching a Valid http:// URL

$url = 'http://www.tutorialcadet.com';
 
if (!preg_match('/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/', $url)) {
 
echo 'The URL you entered was invalid. Be sure to include http://';
 
}


PHP

Matching a US Phone Number

$phonenumber = '555-555-5555';
 
if(preg_match("/^[0-9]{3,3}[-]{1,1}[0-9]{3,3}[-]{1,1}[0-9]{4,4}$/", $phonenumber)) {
   echo $phonenumber;
 }

PHP

Matching a UK Phone Number

$ukphonenumber = '55555555555'; 
if(preg_match("/^[0-9]{11,11}$/", $ukphonenumber)) { 
  echo $ukphonenumber; 
}


PHP

Matching a UK Postal code

$postal = 'AL42PT';
 
if(preg_match("/^[A-Z]{1,2}([0-9]{1,2}|[0-9]{1,1}[A-Z]{1,1})( |)[0-9]{1,1}[A-Z]{2,2}$/", $postal)) {
 
  echo $postal;
 
}

PHP

Matching a US Zip Code

$zip = '55416';
 
if(preg_match("/^[0-9]{5,5}$/", $zip)) {
   echo $zip;
 }


PHP

Matching an IPv4 IP address

$ip = '233.122.122.255';
 
if(preg_match("/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/", $ip)) {
 
  echo $ip;
 
}


PHP

Matching an IPv6 IP address

$ip = 'fe80:0000:0000:0000:0204:61ff:fe9d:f156';
 
if(preg_match("/^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(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