php uses filter_var function to determine mailbox url ip format examples
- 2021-12-12 08:17:02
- OfStack
php uses filter_var function to judge mailbox, url, ip format. Share it for your reference, as follows:
Before using php, I didn't know there was a filter filter. At that time, regular expressions were used to judge whether the address formats of mailbox, url and ip were consistent. Later, with the gradual deepening of use, I realized that the built-in function library filter filter can also be used in php to complete these functions.
For the function filter_var, the validation object is returned if the validation passes, otherwise false is returned.
Grammar
filter_var(variable, filter, options)
参数 | 描述 |
---|---|
variable | 必需。规定要过滤的变量。 |
filter | 可选。规定要使用的过滤器的 ID。默认是 FILTER_SANITIZE_STRING。参见 完整的 PHP Filter 参考手册,查看可能的过滤器。 过滤器 ID 可以是 ID 名称(比如 FILTER_VALIDATE_EMAIL)或 ID 号(比如 274)。 |
options | 可选。规定1个包含标志/选项的关联数组或者1个单1的标志/选项。检查每个过滤器可能的标志和选项。 |
Example
Judge mailbox
<?php
$email = 'fengdingbo@gmail.com';
$result = filter_var($email, FILTER_VALIDATE_EMAIL);
var_dump($result);
Output:
string(20) "fengdingbo@gmail.com"
Judge url
<?php
$url = "http://www.fengdingbo.com";
$result = filter_var($url, FILTER_VALIDATE_URL);
var_dump($result);
Output:
string(25) "http://www.fengdingbo.com"
Judge ip
<?php
$url = "192.168.1.110";
$result = filter_var($url, FILTER_VALIDATE_IP);
var_dump($result);
Output:
string(13) "192.168.1.110"
For more readers interested in PHP related contents, please check the topics of this site: "Summary of Common Functions and Skills of php", "Summary of Usage of php String (string)", "Encyclopedia of Operation Skills of PHP Array (Array)", "Introduction to Basic Grammar of PHP", "Introduction to Database Operation of php+mysql" and "Summary of Operation Skills of Common Database of php"
I hope this article is helpful to everyone's PHP programming.