PHP validation/regex for URL

后端 未结 21 2049
青春惊慌失措
青春惊慌失措 2020-11-22 01:19

I\'ve been looking for a simple regex for URLs, does anybody have one handy that works well? I didn\'t find one with the zend framework validation classes and have seen sev

21条回答
  •  有刺的猬
    2020-11-22 01:53

    Here's a simple class for URL Validation using RegEx and then cross-references the domain against popular RBL (Realtime Blackhole Lists) servers:

    Install:

    require 'URLValidation.php';
    

    Usage:

    require 'URLValidation.php';
    $urlVal = new UrlValidation(); //Create Object Instance
    

    Add a URL as the parameter of the domain() method and check the the return.

    $urlArray = ['http://www.bokranzr.com/test.php?test=foo&test=dfdf', 'https://en-gb.facebook.com', 'https://www.google.com'];
    foreach ($urlArray as $k=>$v) {
    
        echo var_dump($urlVal->domain($v)) . ' URL: ' . $v . '
    '; }

    Output:

    bool(false) URL: http://www.bokranzr.com/test.php?test=foo&test=dfdf
    bool(true) URL: https://en-gb.facebook.com
    bool(true) URL: https://www.google.com
    

    As you can see above, www.bokranzr.com is listed as malicious website via an RBL so the domain was returned as false.

提交回复
热议问题