PHP validation/regex for URL

后端 未结 21 1975
青春惊慌失措
青春惊慌失措 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:02
        function validateURL($URL) {
          $pattern_1 = "/^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+.(com|org|net|dk|at|us|tv|info|uk|co.uk|biz|se)$)(:(\d+))?\/?/i";
          $pattern_2 = "/^(www)((\.[A-Z0-9][A-Z0-9_-]*)+.(com|org|net|dk|at|us|tv|info|uk|co.uk|biz|se)$)(:(\d+))?\/?/i";       
          if(preg_match($pattern_1, $URL) || preg_match($pattern_2, $URL)){
            return true;
          } else{
            return false;
          }
        }
    
    0 讨论(0)
  • 2020-11-22 02:08

    I've used this one with good success - I don't remember where I got it from

    $pattern = "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i";
    
    0 讨论(0)
  • 2020-11-22 02:08

    Peter's Regex doesn't look right to me for many reasons. It allows all kinds of special characters in the domain name and doesn't test for much.

    Frankie's function looks good to me and you can build a good regex from the components if you don't want a function, like so:

    ^(http://|https://)(([a-z0-9]([-a-z0-9]*[a-z0-9]+)?){1,63}\.)+[a-z]{2,6}
    

    Untested but I think that should work.

    Also, Owen's answer doesn't look 100% either. I took the domain part of the regex and tested it on a Regex tester tool http://erik.eae.net/playground/regexp/regexp.html

    I put the following line:

    (\S*?\.\S*?)
    

    in the "regexp" section and the following line:

    -hello.com

    under the "sample text" section.

    The result allowed the minus character through. Because \S means any non-space character.

    Note the regex from Frankie handles the minus because it has this part for the first character:

    [a-z0-9]
    

    Which won't allow the minus or any other special character.

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

    Here is the way I did it. But I want to mentoin that I am not so shure about the regex. But It should work thou :)

    $pattern = "#((http|https)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|”|\"|'|:|\<|$|\.\s)#i";
            $text = preg_replace_callback($pattern,function($m){
                    return "<a href=\"$m[1]\" target=\"_blank\">$m[1]</a>$m[4]";
                },
                $text);
    

    This way you won't need the eval marker on your pattern.

    Hope it helps :)

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

    Edit:
    As incidence pointed out this code has been DEPRECATED with the release of PHP 5.3.0 (2009-06-30) and should be used accordingly.


    Just my two cents but I've developed this function and have been using it for a while with success. It's well documented and separated so you can easily change it.

    // Checks if string is a URL
    // @param string $url
    // @return bool
    function isURL($url = NULL) {
        if($url==NULL) return false;
    
        $protocol = '(http://|https://)';
        $allowed = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)';
    
        $regex = "^". $protocol . // must include the protocol
                 '(' . $allowed . '{1,63}\.)+'. // 1 or several sub domains with a max of 63 chars
                 '[a-z]' . '{2,6}'; // followed by a TLD
        if(eregi($regex, $url)==true) return true;
        else return false;
    }
    
    0 讨论(0)
  • 2020-11-22 02:09

    I've found this to be the most useful for matching a URL..

    ^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$
    
    0 讨论(0)
提交回复
热议问题