PHP verifies whether the mailbox is valid

In the process of website development, it may be necessary to use PHP to verify whether the mailbox format is correct, so we can use the filter_var function in PHP to do so.

 PHP verifies whether the mailbox is valid

Next, we will introduce how PHP can verify whether the mailbox is qualified with specific code examples.

The code example is as follows:

 

 

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";

 

 

 

Here we have created a check_email method to judge whether the mailbox is qualified. If it is qualified, it returns true; otherwise, it returns false.

The output is as follows:

true

false

 

Correlation function:

Filter_var() function - filter a variable with a specific filter

filter_var ( mixed $variable[, int $filter= FILTER_DEFAULT [, mixed $options]] ) : mixed

The parameter variable indicates the variable to be filtered. Note: Scalar values will be converted to strings before filtering.

The filter indicates that the filter ID is to be applied. If omitted, FILTER_DEFAULT is used, which is equivalent to FILTER_UNSAFE_RAW. This will result in no filtering by default.

Options represents an associative array of options, or a bit specific identifier. If the filter accepts the options, these flags can be provided through the "flags" bit of the array. For callback type filters, call should be passed in. This callback function must accept a parameter, that is, the value to be filtered, and return a value after filtering/purification.

The trim () function means to remove the blank characters (or other characters) at the beginning and end of the string

trim ( string $str [, string $character_mask = " \t\n\r\0\x0B" ] ) : string

 

This function returns the result of removing the first and last white space characters from the string str. If you do not specify the second parameter, trim() will remove these characters:

 

"" (ASCII 32 (0x20)), normal space character.

" T" (ASCII 9 (0x09)), tab.

" N" (ASCII 10 (0x0A)), line feed.

" R" (ASCII 13 (0x0D)), carriage return.

" 0" (ASCII 0 (0x00)), empty byte character.

" X0B" (ASCII 11 (0x0B)), vertical tab.

 

 

The parameter str represents the string to be processed.

The character_mask is an optional parameter. The filter character can also be specified by the character_mask parameter. Generally, all the characters you want to filter should be listed. You can also use ".." to list a range of characters.

The return value is a filtered string.

 Watson Blog
  • This article is written by Published on April 25, 2019 08:29:43
  • This article is collected and sorted by the website of Mutual Benefit, and the email address for problem feedback is: wosnnet@foxmail.com , please keep the link of this article for reprinting: https://wosn.net/1889.html

Comment