The filter function in php verifies the mailbox, url, and IP address instance.

Source: Internet
Author: User
Tags preg regular expression

When I used php in my early years, I still didn't know the filter feature. At that time, I used regular expressions to determine whether the mailbox, url, and IP address format were consistent. Later, with the gradual development of the application, we realized that the built-in function library filter can also be used in php to complete these functions.

1. Verify email

First, let's look at the original regular expression verification.

The code is as follows: Copy code

<? Php
Function isEmail ($ email ){
If (preg_match ("/^ [0-9a-zA-Z] + @ ([0-9a-zA-Z] +) [.]) + [a-z] {2, 4} $/I ", $ email )){
Return 'mailbox verification OK ';
} Else {
Return 'verification is not a mailbox ';
 }
}
?>

Let's look at the filter.

The code is as follows: Copy code

$ Email = 'sjlinyu @ qq.com ';

$ Result = filter_var ($ email, FILTER_VALIDATE_EMAIL );

Var_dump ($ result); // string (14) "sjlinyu@qq.com"

For the filter_var function, if the verification succeeds, the verification object is returned; otherwise, false is returned.

I think the latter is simpler.

2. Verify the url address

Regular expression verification

The code is as follows: Copy code

<? Php
$ Url = 'www .111cn.net ';
$ Search = '/--- regular N ---/';
If (preg_match ($ search, $ url )){
Echo 'matched ';
} Else {
Echo 'mismatched ';
}
?>

Filter_var function

The code is as follows: Copy code

$ Url = "http://www.111cn.net ";

$ Result = filter_var ($ url, FILTER_VALIDATE_URL );

Var_dump ($ result); // string (22) "http://www.111cn.net"

3. Verify the IP address

Regular expression verification function

The code is as follows: Copy code

/**
* Check whether the IP address is correct.
*/
Function checkipaddres ($ ipaddres ){
$ Preg = "/A ([0-9]? [0-9]) | (1 [0-9] {2}) | (2 [0-4] [0-9]) | (25 [0-5]).) {3} ([0-9]? [0-9]) | (1 [0-9] {2}) | (2 [0-4] [0-9]) | (25 [0-5]) Z /";
If (preg_match ($ preg, $ ipaddres) return true;
Return false;
}


 

Verify this function

The code is as follows: Copy code

$ Url = "192.168.1.110 ";

$ Result = filter_var ($ url, FILTER_VALIDATE_IP );

Var_dump ($ result); // string (13) "192.168.1.110"

This method can also be used to verify ipv6.

 

The code is as follows: Copy code

$ Url = "2001: DB8: 2de: e13 ";

$ Result = filter_var ($ url, FILTER_VALIDATE_IP );

Var_dump ($ result); // string (17) "2001: DB8: 2de: e13"

Verify whether the value is an integer within an integer range

The code is as follows: Copy code


$ I = '010 ';

$ Result = filter_var (

$ I,

FILTER_VALIDATE_INT,

// Set the value range for verification

Array (

'Options' => array ('min _ range' => 1, 'max _ range' => 100)

     ) 

);

Var_dump ($ result); // bool (false)

Php variables are of a weak type. If a filter is not used, it is true to use a variable greater than or less than the symbol.

The code is as follows: Copy code


$ I = '010 ';

$ Result = $ I >= 1 & $ I <= 100;

Var_dump ($ result); // bool (true)

5. Verify floating point number

The code is as follows: Copy code


$ Float = 12.312;

$ Result = filter_var ($ float, FILTER_VALIDATE_FLOAT );

Var_dump ($ result); // float (12.312)

When verifying the amount, you often need to verify whether the amount is a floating point number.

Summary

Although the filter in php is relatively unpopular, it is quite powerful. In addition to the above features, there are also some filtering input functions, you can refer to the php Manual.

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.