/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
Matching a Valid Email Address view source print? $email = '[email protected]' if (!preg_match('#[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{1,10}#i', $email)) { echo 'The email address you entered was invalid.'; } Matching a string between 4 and 128 characters view source print? $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 view source print? $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. view source print? $html = '<style class="sdfgsd">this is the matched pattern</style>'; preg_match('/<style(.*)?>(.*)?<\/style>/', $html, $match); print $match[2]; Matching a Valid http:// URL view source print? $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 <strong>http://</strong>'; } Matching a US Phone Number view source print? $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 view source print? $ukphonenumber = '01614840484'; if(preg_match("/^[0-9]{11,11}$/", $ukphonenumber)) { echo $ukphonenumber; } Matching a UK Postal code view source print? $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 view source print? $zip = '55416'; if(preg_match("/^[0-9]{5,5}$/", $zip)) { echo $zip; } Matching an IPv4 IP address view source print? $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 view source print? <?php $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}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[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}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[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}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[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}))|:)))(%.+)?\s*$/", $ip)) { echo $ip; } ?>
URL: http://www.tutorialcadet.com/10-useful-regex-patterns-for-php/