PHP各种正则表达式罗列
//验证用户名的正则
$user_name = ‘user_name12‘;
if (preg_match(‘/^[a-z\d_]{5,20}$/i‘, $user_name)) {
echo ‘your username is ok‘ . PHP_EOL . ‘<br>‘;
} else {
echo ‘your username is error‘;
}
//验证日期的正则
$date = date(‘Y-m-d‘);
if (preg_match(‘/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/i‘, $date)) {
echo ‘date is ok‘ . PHP_EOL . ‘<br>‘;
} else {
echo ‘date is error‘;
}
//验证邮政编码
$zipcode = "12354-5432";
if (preg_match(‘/^([0-9]{5})(-[0-9]{4})?$/i‘, $zipcode)) {
echo ‘zipcode is ok‘ . PHP_EOL . ‘<br>‘;
} else {
echo ‘zipcode is error‘;
}
//手机号码验证
$tel = "15043947153";
if (preg_match(‘/^0?(13[0-9]|15[012356789]|17[0123456789]|18[0-9]|14[0-9])[0-9]{8}$/i‘, $tel)) {
echo ‘tel is ok‘ . PHP_EOL . ‘<br>‘;
} else {
echo ‘tel is error‘;
}
//邮箱验证
$email = "[email protected]";
if (preg_match("/^[0-9a-zA-Z]+@(([0-9a-zA-Z]+)[.])+[a-z]{2,4}$/i", $email)) {
echo ‘email is ok‘ . PHP_EOL . ‘<br>‘;
} else {
echo ‘email ie error‘;
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。