Provides various official and user-released code examples and code reference. You are welcome to exchange and learn how to use them in your usual projects. php built-in functions!
1. verify email
$ 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.
2. Verify the url address
$ Url = "http://www.baidu.com ";
$ Result = filter_var ($ url, FILTER_VALIDATE_URL );
Var_dump ($ result); // "http://www.baidu.com"
3. Verify the IP address
$ 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
$ Url = "2001: DB8: 2de: e13 ";
$ Result = filter_var ($ url, FILTER_VALIDATE_IP );
Var_dump ($ result); // string (17) "2001: DB8: 2de: e13"
4. Check whether the value is an integer within an integer range.
$ 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.
$ I = '010 ';
$ Result = $ I >= 1 & $ I <= 100;
Var_dump ($ result); // bool (true)
5. Verify floating point number
$ 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.
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB