メールアドレスかどうか調べる
先に言っておきますが、正規表現でメールアドレスかどうかを調べるのは大変難しく、少なくともメールアドレスらしいもので振り分けます。
if (preg_match("/^([*+!.&#$|'¥¥%¥/0-9a-z^_`{}=?:-]+)@([0-9a-z-]+¥.)+[0-9a-z]{2,}$/i", $text)) { echo "正しいメールアドレスです"; } else { echo "不正なメールアドレスです"; }
より正確にしようとすると下記のように長くなりますが、RFC821,RFC822によるとメールアドレスはクオートすれば、ASCII文字なら(NULLを除く)なんでも使えます。
[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x 80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥ ¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*(?:(?:[^(¥040)<>@,;:".¥¥¥[¥]¥000 -¥037¥x80-¥xff]+(?![^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff])|"[^¥ ¥¥x80-¥xff¥n¥015"]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015"]*)*")[¥04 0¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥ xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80 -¥xff¥n¥015()]*)*¥)[¥040¥t]*)*(?:¥.[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥0 15()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥x ff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t] *)*(?:[^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff]+(?![^(¥040)<>@,;:" .¥¥¥[¥]¥000-¥037¥x80-¥xff])|"[^¥¥¥x80-¥xff¥n¥015"]*(?:¥¥[^¥x80-¥xf f][^¥¥¥x80-¥xff¥n¥015"]*)*")[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*( ?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥ ¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*)*@[ ¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x8 0-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥ x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*(?:[^(¥040)<>@,;:".¥¥¥[¥]¥000-¥03 7¥x80-¥xff]+(?![^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff])|¥[(?:[^¥ ¥¥x80-¥xff¥n¥015¥[¥]]|¥¥[^¥x80-¥xff])*¥])[¥040¥t]*(?:¥([^¥¥¥x80-¥x ff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥ x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥ 040¥t]*)*(?:¥.[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80- ¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥01 5()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*(?:[^(¥040)<>@,;:" .¥¥¥[¥]¥000-¥037¥x80-¥xff]+(?![^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80- ¥xff])|¥[(?:[^¥¥¥x80-¥xff¥n¥015¥[¥]]|¥¥[^¥x80-¥xff])*¥])[¥040¥t]*( ?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥ 015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥ n¥015()]*)*¥)[¥040¥t]*)*)*|(?:[^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80- ¥xff]+(?![^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff])|"[^¥¥¥x80-¥xff ¥n¥015"]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015"]*)*")[^()<>@,;:".¥¥ ¥[¥]¥x80-¥xff¥000-¥010¥012-¥037]*(?:(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(? :(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥ x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)|"[^¥¥¥x80-¥xff¥ n¥015"]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015"]*)*")[^()<>@,;:".¥¥¥ [¥]¥x80-¥xff¥000-¥010¥012-¥037]*)*<[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥0 15()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥x ff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t] *)*(?:@[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥ ([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)* ¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*(?:[^(¥040)<>@,;:".¥¥¥[¥] ¥000-¥037¥x80-¥xff]+(?![^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff])| ¥[(?:[^¥¥¥x80-¥xff¥n¥015¥[¥]]|¥¥[^¥x80-¥xff])*¥])[¥040¥t]*(?:¥([^¥ ¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]* (?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015() ]*)*¥)[¥040¥t]*)*(?:¥.[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥ ¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥ xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*(?:[^(¥040 )<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff]+(?![^(¥040)<>@,;:".¥¥¥[¥]¥000-¥ 037¥x80-¥xff])|¥[(?:[^¥¥¥x80-¥xff¥n¥015¥[¥]]|¥¥[^¥x80-¥xff])*¥])[¥ 040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80 -¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x 80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*)*(?:,[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥ n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80 -¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040 ¥t]*)*@[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥ ([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)* ¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*(?:[^(¥040)<>@,;:".¥¥¥[¥] ¥000-¥037¥x80-¥xff]+(?![^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff])| ¥[(?:[^¥¥¥x80-¥xff¥n¥015¥[¥]]|¥¥[^¥x80-¥xff])*¥])[¥040¥t]*(?:¥([^¥ ¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]* (?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015() ]*)*¥)[¥040¥t]*)*(?:¥.[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥ ¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥ xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*(?:[^(¥040 )<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff]+(?![^(¥040)<>@,;:".¥¥¥[¥]¥000-¥ 037¥x80-¥xff])|¥[(?:[^¥¥¥x80-¥xff¥n¥015¥[¥]]|¥¥[^¥x80-¥xff])*¥])[¥ 040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80 -¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x 80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*)*)*:[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n ¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80- ¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥ t]*)*)?(?:[^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff]+(?![^(¥040)<>@ ,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff])|"[^¥¥¥x80-¥xff¥n¥015"]*(?:¥¥[^¥x80 -¥xff][^¥¥¥x80-¥xff¥n¥015"]*)*")[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015( )]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff] [^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)* (?:¥.[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([ ^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥) )[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*(?:[^(¥040)<>@,;:".¥¥¥[¥]¥0 00-¥037¥x80-¥xff]+(?![^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff])|"[ ^¥¥¥x80-¥xff¥n¥015"]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015"]*)*")[¥ 040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80 -¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x 80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*)*@[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥0 15()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥x ff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t] *)*(?:[^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff]+(?![^(¥040)<>@,;:" .¥¥¥[¥]¥000-¥037¥x80-¥xff])|¥[(?:[^¥¥¥x80-¥xff¥n¥015¥[¥]]|¥¥[^¥x80 -¥xff])*¥])[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xf f]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015() ]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*(?:¥.[¥040¥t]*(?:¥([^ ¥¥¥x80-¥xff¥n¥015()]*(?:(?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()] *(?:¥¥[^¥x80-¥xff][^¥¥¥x80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015( )]*)*¥)[¥040¥t]*)*(?:[^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff]+(?! [^(¥040)<>@,;:".¥¥¥[¥]¥000-¥037¥x80-¥xff])|¥[(?:[^¥¥¥x80-¥xff¥n¥01 5¥[¥]]|¥¥[^¥x80-¥xff])*¥])[¥040¥t]*(?:¥([^¥¥¥x80-¥xff¥n¥015()]*(?: (?:¥¥[^¥x80-¥xff]|¥([^¥¥¥x80-¥xff¥n¥015()]*(?:¥¥[^¥x80-¥xff][^¥¥¥x 80-¥xff¥n¥015()]*)*¥))[^¥¥¥x80-¥xff¥n¥015()]*)*¥)[¥040¥t]*)*)*>)
関連記事
- Smarty2をPHP7に対応させる方法(The /e modifier is no longer supported Smarty_Compiler.class.php, line 270)
- 全て携帯絵文字のみかどうかを調べる
- PHPの正規表現
- 携帯電話のメールアドレスか調べる
- 正しい電話番号か調べる
- 正しい郵便番号か調べる
- 正しいURLかどうか調べる
- すべて漢字かどうか調べる
- すべてカタカナかどうか調べる
- すべて「ひらがな」かどうか調べる
スポンサーリンク