php使用filter过滤器验证邮箱 ipv6地址 url验证_PHP

WBOY
Release: 2016-06-01 11:58:06
Original
1406 people have browsed it

IPv6

1、验证邮箱

复制代码 代码如下:
$email = 'jb51@qq.com';
$result = filter_var($email, FILTER_VALIDATE_EMAIL);
var_dump($result); //string(14) "jb51@qq.com"

2、验证url地址

复制代码 代码如下:
$url = "http://www.bitsCN.com";
$result = filter_var($url, FILTER_VALIDATE_URL);
var_dump($result); //string(22) "http://www.bitsCN.com"

3、验证ip地址

复制代码 代码如下:
$url = "192.168.1.110";
$result = filter_var($url, FILTER_VALIDATE_IP);
var_dump($result); //string(13) "192.168.1.110"

值的一提的是,这方法也可以用来验证ipv6。

复制代码 代码如下:
$url = "2001:DB8:2de::e13";
$result = filter_var($url, FILTER_VALIDATE_IP);
var_dump($result); //string(17) "2001:DB8:2de::e13"

4、验证数值是否为整数,并且在一个整数区间内

复制代码 代码如下:
$i = '010';
$result = filter_var(
    $i,
    FILTER_VALIDATE_INT,
    //设定校验的数值范围
    array(
      'options' => array('min_range' => 1, 'max_range' => 100)
    )
);
var_dump($result);//bool(false)

php的变量是弱类型,如果不用过滤器,直接使用大于小于符号判断的话会是真的。

复制代码 代码如下:
$i = '010';
$result = $i >= 1 && $i var_dump($result);//bool(true)

5、验证浮点数

复制代码 代码如下:
$float = 12.312;
$result = filter_var($float, FILTER_VALIDATE_FLOAT);
var_dump($result); //float(12.312)

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!