$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.';
}
$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.';
}
$html = '<style class="sdfgsd">this is the matched pattern</style>';
preg_match('/<style(.*)?>(.*)?<\/style>/', $html, $match);
print $match[2];
$url = https://www.pwnedgamers.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>https://</strong>';
}
$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;
}
$ukphonenumber = '01614840484';
if(preg_match("/^[0-9]{11,11}$/", $ukphonenumber)) {
echo $ukphonenumber;
}
$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;
}
$zip = '55416';
if(preg_match("/^[0-9]{5,5}$/", $zip)) {
echo $zip;
}
$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][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