티스토리 뷰
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.'; |
} |
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 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 <strong>http://</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}:){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; |
} |
?>'웹개발 > Php' 카테고리의 다른 글
| OAuth 정리 (0) | 2012.01.19 |
|---|---|
| PHP 파일 모드 간단 설명 (0) | 2012.01.11 |
| Kohana Validation 체크 (0) | 2011.08.26 |
| Kohana Query Builder (0) | 2011.08.26 |
| Kohana Request (0) | 2011.08.26 |
댓글