0

Possible Duplicate:
How to validate domain name in PHP?
Better way to validate a URL in PHP

How do I check, if $variable is a site address?

Like, for this it should give true:

$varialbe = 'http://google.com';

For this false:

$variable = 'this value can be anything, but we know its not a domain';
Community
  • 1
  • 1
Jasper
  • 5,090
  • 11
  • 34
  • 41
  • 5
    You should probably consult this strange little fellow named Google. Ask him about `php url validation`, he might know. – Dan Lugg Dec 20 '11 at 06:33

1 Answers1

5

Use the filter_var function (also see types of filters) provided by PHP:

$is_url = filter_var($url, FILTER_VALIDATE_URL);
nikc.org
  • 16,462
  • 6
  • 50
  • 83
  • It has some problems but is a good enough solution for his need, IMO – Lucky Murari Dec 20 '11 at 06:43
  • @LuckyMurari: Indeed, you might be best off combining some further validation, but it's a good starting point none-the-less. The comments in the PHP manual will point out the quirks, or some of them anyhow. (The PHP manual is quite underrated and needs to be promoted more.) – nikc.org Dec 20 '11 at 06:47
  • @nikc -- The documentation has it's own quirks though; inconsistencies in method signatures, missing noteworthy edge cases that become all to apparent very quickly, etc. Either way, good answer; I've not used the filter functions yet anywhere. – Dan Lugg Dec 20 '11 at 07:12