正则表达式检测邮箱地址是否正确
题目如题了……
这个正则完全抄书……所以不多解释。
使用的函数是preg_match
,在PHP 实现中文字符串截取无乱码提及了就不怎么解释了。
电子邮件的正则表达式是:/^\w+@\w+(\.\w+){0,3}$/
源码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Check Email</title>
</head>
<body>
<form action="check_email.php" method="post">
<input type="text" name="email" placeholder="输入email">
<input type="submit" value="提交">
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$problem = FALSE;
$email = $_POST['email'];
if (empty($email)) {
$problem = TRUE;
echo '请输入邮箱';
}
if (!$problem) {
checkEmail($email);
}
}
function checkEmail($email) {
$pattern = '/^\w+@\w+(\.\w+){0,3}$/';
if (preg_match($pattern, $email, $matches)) {
echo 'All right';
} else {
echo 'Sorry,Wrong address';
}
}
?>
</body>
</html>
植入部分
如果您觉得文章不错,可以通过赞助支持我。
如果您不希望打赏,也可以通过关闭广告屏蔽插件的形式帮助网站运作。
我偏爱
filter_var()
函数