1

I need to check if a form field is a valid URL (it can be done through HTML5, but not every browser has it yet), and I can't find a good URL checker function. It's strange, because with email-checking, there are lots of already-done functions.

I've seen (I just discovered it) the filter_var() function, but as I read, there are some issues with urls. I can find some regular expressions, but they don't seem very exhaustive.

Do you know any good url checker function?

markmb
  • 852
  • 4
  • 12
  • 32
  • The issue with filter_var() is only found in PHP 5.2.13 or older If you are using a newer version of PHP then I suggest using filter_var() – Ali Aug 28 '13 at 22:00

3 Answers3

1

It's because URL format can be so versatile :

Http// is optionnal
www is optionnal
.com, .net, .us, etc so many possible pattern
.html, /, .php, or nothing are possible ends

Rather than using a pre-made function, I'd recommend you to build your own function depending on how most of your users send URL, with which ending etc ...

My two cents.

Clément Malet
  • 5,062
  • 3
  • 29
  • 48
1

Let's go a little advanced:

<?php 
function validate_url($url) {
  return (bool)preg_match("
      /^                                                      # Start at the beginning of the text
      (?:ftp|https?|feed):\/\/                                # Look for ftp, http, https or feed schemes
      (?:                                                     # Userinfo (optional) which is typically
        (?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)*      # a username or a username and password
        (?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@          # combination
      )?
      (?:
        (?:[a-z0-9\-\.]|%[0-9a-f]{2})+                        # A domain name or a IPv4 address
        |(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\])         # or a well formed IPv6 address
      )
      (?::[0-9]+)?                                            # Server port number (optional)
      (?:[\/|\?]
        (?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})   # The path and query (optional)
      *)?
    $/xi", $url);
}
?>

Mainly from valid_url function in Drupal.

Example use:

validate_url($url); // this will return TRUE if the URL is correct. Protocol should exist in $url
AKS
  • 4,618
  • 2
  • 29
  • 48
0

Or try a different aproach.

Why not check with file_get_contents($url) ? This way you will also know if the URL works, or not. I guess you could use something like fread(fopen($url,'r'),100); so it can not be injected with some huge file. Checking for things like localhost, is still something you might want to do.