各种正则表达式罗列

//验证用户名的正则
$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 = "13225@qq.com";
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';
}

posted @ 2015-04-23 17:05  php点点滴滴  阅读(185)  评论(0编辑  收藏  举报