• php使用过滤器filter_var轻松验证邮箱url和ip地址等


    以前使用php的时候还不知道有过滤器filter这玩意,那时候判断邮箱、url和ip地址格式是否符合都是用正则表达式。后来随着使用的逐渐深入,才知道在php中也可以使用内置的函数库过滤器filter来完成这些功能。 对于filter_var这个函数,如果验证通过则会返回验证对象,否则返回false。 php验证邮箱

    <?php
    $email = 'fengdingbo@gmail.com';                                                
    $result = filter_var($email, FILTER_VALIDATE_EMAIL);
    var_dump($result); // string(20) "fengdingbo@gmail.com"
    php验证url地址
    <?php
    $url = "http://www.fengdingbo.com";
    $result = filter_var($url, FILTER_VALIDATE_URL);
    var_dump($result); // string(25) "http://www.fengdingbo.com"

    php验证ip地址

    <?php
    $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"
  • 相关阅读:
    js使用笔记
    rabbit-mq使用官方文档
    tomcat Enabling JMX Remote
    Venom的简单使用
    Random模块
    时间模块
    shulti模块简述
    Python的os模块
    Python压缩及解压文件
    Kali的内网穿透
  • 原文地址:https://www.cnblogs.com/xlz307/p/3428249.html
Copyright © 2020-2023  润新知