Post: 10++ Useful Regex Patterns for PHP
04-29-2011, 12:56 AM #1
(adsbygoogle = window.adsbygoogle || []).push({}); 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 = '[email protected]'

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.';

}

Matching a string between 4 and 128 characters

    


$string = "tutorial";

if (!preg_match('#^(.){4,128}$#', $string)) {

echo 'The string you entered is invalid.';

}

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.';

}

Matching Content between any given HTML tag with attributes

simply replace “style” with HTML tag your trying to match.


    
$html = '<style class="sdfgsd">this is the matched pattern</style>';

preg_match('/<style(.*)?>(.*)?<\/style>/', $html, $match);

print $match[2];



Matching a Valid https:// URL




    $url = https://www.pwnedgamers.com

if (!preg_match('/^(([\w]+Smile?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+Upside Down Happy([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}Upside Down Happy[\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 <strong>https://</strong>';

}

Matching a US Phone Number




    $phonenumber = '333-333-3333';

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

Matching a UK Phone Number




    $ukphonenumber = '01614840484';

if(preg_match("/^[0-9]{11,11}$/", $ukphonenumber)) {

echo $ukphonenumber;

}

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;

}

Matching a US Zip Code




    $zip = '55416';

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

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;

}

Matching an IPv6 IP address




    <?php

$ip = 'fe80:0000:0000:0000:0204:61ff:fe9d:f156';

if(preg_match("/^\s*((([0-9A-Fa-f]{1,4}Smile{7}([0-9A-Fa-f]{1,4}|Smile)|(([0-9A-Fa-f]{1,4}Smile{6}Upside Down Happy[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})|Smile)|(([0-9A-Fa-f]{1,4}Smile{5}((Upside Down Happy[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})|Smile)|(([0-9A-Fa-f]{1,4}Smile{4}((Upside Down Happy[0-9A-Fa-f]{1,4}){1,3})|(Upside Down Happy[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}))|Smile)|(([0-9A-Fa-f]{1,4}Smile{3}((Upside Down Happy[0-9A-Fa-f]{1,4}){1,4})|(Upside Down Happy[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}))|Smile)|(([0-9A-Fa-f]{1,4}Smile{2}((Upside Down Happy[0-9A-Fa-f]{1,4}){1,5})|(Upside Down Happy[0-9A-Fa-f]{1,4}){0,3}:((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}))|Smile)|(([0-9A-Fa-f]{1,4}Smile{1}((Upside Down Happy[0-9A-Fa-f]{1,4}){1,6})|(Upside Down Happy[0-9A-Fa-f]{1,4}){0,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}))|Smile)|Upside Down Happy((Upside Down Happy[0-9A-Fa-f]{1,4}){1,7})|(Upside Down Happy[0-9A-Fa-f]{1,4}){0,5}:((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}))|Smile))(%.+)?\s*$/", $ip)) {
echo $ip;
}

?>



This is not copy and pasted no source i wrote it :angel:
(adsbygoogle = window.adsbygoogle || []).push({});

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo