Php: built-in method for verifying whether the mailbox, URL, and IP address are valid

Source: Internet
Author: User
Tags encode string
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.