验证email地址的格式是否正确,代码如下:
<?php /** * Email验证类 * 根据正则验证email格式是否正确 * N.B. 'a' in 'a_email' stands for Alberon not the pronoun! * */ class a_email { /** * 电子邮件地址 * * @var string */ var $email; /** * 错误消息 * * @var string */ var $error_html; /** * 构造函数 * * @param string $email * @return null */ function a_email($email) { $this->email = $email; } /** * 验证email格式 */ function email_is_valid() { $valid = true; if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,8})$", $this->email)) { $valid = false; $this->error_html = "$this->email is not properly formatted"; } return $valid; } } //调用示例 $tmp_mail = "test123@jb200.com" $email = new a_email(); if ($email->email_is_valid()){ echo "email格式正确"; }else{ echo "对不起,您输入的email有误."; }