<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>メールアドレスの形式をチェックしたい</title>
</head>
<body>
<?php
if (isset($_POST['email'])) {
$email = $_POST['email'];
$pattern =
'/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/iD';
if (preg_match($pattern, $email)) {
echo '<p>「' . h($email) . '」はメールアドレスとして正しい形式です。</p>';
} else {
echo '<p>「' . h($email) .
'」はメールアドレスとして正しい形式ではありません。</p>';
}
}
function h($string) {
return htmlspecialchars($string, ENT_QUOTES);
}
?>
<form method="post" action="">
<p>メールアドレスを入力してください</p>
<input type="text" name="email" value="" />
<input type="submit" value="確認する" />
</form>
</body>
</html>