Home  >  Article  >  Backend Development  >  How to use PHP to verify whether the email is qualified

How to use PHP to verify whether the email is qualified

autoload
autoloadOriginal
2021-03-25 09:52:532023browse

How to use PHP to verify whether the email is qualified

本文主要利用的相关函数filter_var()trim()函数来判断邮箱是否正确。

1.相关函数的语法知识:

    filter_var()语法格式:

filter_var ( mixed $value , int $filter = FILTER_DEFAULT , array|int $options = 0 ) : mixed
  • $value要过滤的变量。

  • $filter要使用的过滤器的 ID,本文是验证邮件的,所以使用的是 FILTER_VALIDATE_EMAIL ,ID 为274。

  • $options一个包含标志/选项的关联数组或者一个单一的标志/选项。

返回值:返回过滤后的数据,如果过滤失败则返回 false 。

    trim()语法格式:

trim ( string $str , string $character_mask = " \t\n\r\0\x0B" ) : string
  • $str待处理的字符串。

  • $character_mask为可选参数,默认值为“\t\n\r\0\x0B”,过滤字符也可由 $character_mask 参数指定。一般要列出所有希望过滤的字符,也可以使用 “..” 列出一个字符范围。

返回值:此函数返回字符串 str 去除首尾空白字符后的结果。如果不指定第二个参数,trim() 将去除$character_mask的默认值。

2.使用的示例:

<?php
function check_email($email){
    $result = trim($email);
 if (filter_var($result, FILTER_VALIDATE_EMAIL))
   {
      return "true";
   }else{
     return "false";
 }
 }
 echo check_email("111@qq.com")."\n";
 echo check_email("abc#example.com")."\n";

推荐:《php视频教程》《php教程

The above is the detailed content of How to use PHP to verify whether the email is qualified. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn