正则表达式 手机号和邮箱

手机:

    preg_match('/^1[3456789]{1}\d{9}$/',$phone

邮箱:

PHP 邮箱正则表达式代码如下:

/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/

实例

<?php
   $mail = 'runoob@runoob.com';
  //邮箱地址
   $pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/";
   preg_match($pattern, $mail, $matches);
   var_dump($matches); //输出匹配结果
?>

以上代码运行输出结果为:

array(4) {
  [0]=>
  string(17) "runoob@runoob.com"
  [1]=>
  string(0) ""
  [2]=>
  string(0) ""
  [3]=>
  string(4) ".com"
}
 

使用 FILTER_VALIDATE_EMAIL 过滤器

实例

<?php
     $email = 'runoob@runoob.com'; //邮箱地址
     if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
          $emailMsg = "正确邮箱格式";
      } else {
         $emailMsg = "非法邮箱格式";
      }
    echo $emailMsg;
 
posted @ 2020-03-12 10:50  笨笨韩  阅读(926)  评论(0)    收藏  举报