PHP validation/regex for URL

后端 未结 21 1974
青春惊慌失措
青春惊慌失措 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 02:13

    The best URL Regex that worked for me:

    function valid_URL($url){
        return preg_match('%^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@|\d{1,3}(?:\.\d{1,3}){3}|(?:(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)(?:\.(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)*(?:\.[a-z\x{00a1}-\x{ffff}]{2,6}))(?::\d+)?(?:[^\s]*)?$%iu', $url);
    }
    

    Examples:

    valid_URL('https://twitter.com'); // true
    valid_URL('http://twitter.com');  // true
    valid_URL('http://twitter.co');   // true
    valid_URL('http://t.co');         // true
    valid_URL('http://twitter.c');    // false
    valid_URL('htt://twitter.com');   // false
    
    valid_URL('http://example.com/?a=1&b=2&c=3'); // true
    valid_URL('http://127.0.0.1');    // true
    valid_URL('');                    // false
    valid_URL(1);                     // false
    

    Source: http://urlregex.com/

    0 讨论(0)
  • 2020-11-22 02:14

    As per John Gruber (Daring Fireball):

    Regex:

    (?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))
    

    using in preg_match():

    preg_match("/(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/", $url)
    

    Here is the extended regex pattern (with comments):

    (?xi)
    \b
    (                       # Capture 1: entire matched URL
      (?:
        https?://               # http or https protocol
        |                       #   or
        www\d{0,3}[.]           # "www.", "www1.", "www2." … "www999."
        |                           #   or
        [a-z0-9.\-]+[.][a-z]{2,4}/  # looks like domain name followed by a slash
      )
      (?:                       # One or more:
        [^\s()<>]+                  # Run of non-space, non-()<>
        |                           #   or
        \(([^\s()<>]+|(\([^\s()<>]+\)))*\)  # balanced parens, up to 2 levels
      )+
      (?:                       # End with:
        \(([^\s()<>]+|(\([^\s()<>]+\)))*\)  # balanced parens, up to 2 levels
        |                               #   or
        [^\s`!()\[\]{};:'".,<>?«»“”‘’]        # not a space or one of these punct chars
      )
    )
    

    For more details please look at: http://daringfireball.net/2010/07/improved_regex_for_matching_urls

    0 讨论(0)
  • 2020-11-22 02:15

    There is a PHP native function for that:

    $url = 'http://www.yoururl.co.uk/sub1/sub2/?param=1&param2/';
    
    if ( ! filter_var( $url, FILTER_VALIDATE_URL ) ) {
        // Wrong
    }
    else {
        // Valid
    }
    

    Returns the filtered data, or FALSE if the filter fails.

    Check it here

    0 讨论(0)
提交回复
热议问题