PHP verifies whether the mailbox, URL, and IP address are valid. Previously, PHP verifies whether the mailbox, URL, and IP address are valid through self-writing regular expressions, it is possible to write a regular expression that is not completely correct, leading to verification errors. Today, it is found that PHP itself contains a function to verify whether the mailbox, URL, and IP address are valid. The main use is filter_var.
PHP verifies whether the mailbox, URL, and IP address are valid. Previously, PHP verifies whether the mailbox, URL, and IP address are valid through self-writing regular expressions, it is possible to write a regular expression that is not completely correct, leading to verification errors. Today, it is found that PHP itself contains a function to verify whether the mailbox, URL, and IP address are valid. The main use is filter_var.
Verify the mailbox, URL, and IP address with PHP
In the past, PHP was used to verify whether the mailbox, URL, and IP address are valid by writing regular expressions. However, sometimes the brain is dizzy and may write an incorrect regular expression, leading to verification errors, today, we found that PHP has built-in functions to verify the validity of email addresses, URLs, and IP addresses.
The filter_var function is mainly used.
Syntax
Filter_var(Variable, filter, options)
Parameter description
Variable is required. Specifies the variable to be filtered.
Optional. Specifies the ID of the filter to be used.
Options specifies an array containing flag/option. Check the possible flags and options of each filter.
PHP Filters
ID |
Description |
FILTER_CALLBACK |
You can call a user-defined function to filter data. |
FILTER_SANITIZE_STRING |
FILTER_SANITIZE_STRIPPED |
The alias of the "string" filter. |
FILTER_SANITIZE_ENCODED |
URL-encode string to remove or encode special characters. |
FILTER_SANITIZE_SPECIAL_CHARS |
HTML Escape Character '"<> & characters with ASCII value less than 32. |
FILTER_SANITIZE_EMAIL |
Delete all characters except letters, numbers, and! # $ % & '* +-/=? ^ _ '{| }~ @. [] |
FILTER_SANITIZE_URL |
Delete all characters except letters, numbers, and $-_. +! * '(), {}|\\^ ~ [] '<> # % ";/? : @ & = |
FILTER_SANITIZE_NUMBER_INT |
Delete all characters except numbers and +- |
FILTER_SANITIZE_NUMBER_FLOAT |
Delete all characters except numbers, +-, and., eE. |
FILTER_SANITIZE_MAGIC_QUOTES |
Apply addslashes (). |
FILTER_UNSAFE_RAW |
Do not filter, remove, or encode special characters. |
FILTER_VALIDATE_INT |
The value is verified by an integer in the specified range. |
FILTER_VALIDATE_BOOLEAN |
If it is "1", "true", "on", and "yes", true is returned. If it is "0", "false", "off ", "no" and "", false is returned. Otherwise, NULL is returned. |
FILTER_VALIDATE_FLOAT |
Verify the value with a floating point number. |
FILTER_VALIDATE_REGEXP |
Verify the value based on regexp and Perl-Compatible Regular Expressions. |
FILTER_VALIDATE_URL |
Use the value as the URL for verification. |
FILTER_VALIDATE_EMAIL |
Use the value as email for verification. |
FILTER_VALIDATE_IP |
Use the value as the IP address for verification. |
Example:
The above routine will output:
string(15) "bob@example.com"bool(false)