Home > Backend Development > PHP Tutorial > Use php to verify whether the email format is correct

Use php to verify whether the email format is correct

王林
Release: 2023-04-08 09:08:01
forward
4114 people have browsed it

Use php to verify whether the email format is correct

Verifying whether the email format is correct can be achieved through the filter_var function.

Function introduction:

filter_var() function filters a variable through the specified filter.

Syntax:

filter_var(variable, filter, options)
Copy after login

Parameter introduction:

variable, (required) specifies the variable to be filtered.

filter, (optional) Specifies the ID of the filter to use. The default is FILTER_SANITIZE_STRING.

options, (optional) Specifies an associative array containing flags/options or a single flag/option. Check the possible flags and options for each filter.

(Free learning video tutorial sharing: php video tutorial)

The verification method is as follows:

<?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";
Copy after login

Here we created a check_email method , used to determine whether the email is qualified. Returns true if qualified, false otherwise.

The output is as follows:

true
false
Copy after login

Recommended related article tutorials: php tutorial

The above is the detailed content of Use php to verify whether the email format is correct. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template