Validate URL Using PHP Regex


There are times you may want a URL validation function that accepts (I think) most known types of URL’s. Useful for validating a homepage link, or submission of links from the public.

It allows for port, path and query string validations, the parameter $url string contains the user input URL and the function returns the boolean of true or false.

<?php
/**
* Validate URL
* Allows for port, path and query string validations
* @param    string      $url	   string containing url user input
* @return   boolean     Returns TRUE/FALSE
*/
function validateURL($url)
{
$pattern = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&amp;?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/';
return preg_match($pattern, $url);
}

$result = validateURL('http://www.google.com');
print $result;
?>

Hope this helps!

  1. 12 Responses to “Validate URL Using PHP Regex”

  2. thanks for this simple yet handy script!

    By m0dk1d on Jun 5, 2008

  3. It doesn’t work for me in PHP 5.2.5

    Warning: preg_match() [function.preg-match]: Unknown modifier ‘[’ in C:\xampp\htdocs\test.php on line 18

    By Ben Wallis on Jun 6, 2008

  4. Hi, Ben

    I have rewritten the code and tested it, now when you run the code above, you should get either ‘1′ or ‘0′ as results stands for ‘true’ or ‘false’.

    Thanks for pointing it out. Do let me know if there is anymore problem. :)

    By admin on Jun 6, 2008

  5. http://www-google-com
    http://www_google_com

    This regex fails in so many ways

    By Not very useful on Jun 11, 2008

  6. Hi, thanks for point out the problems. It’s fixed now.

    By admin on Jun 14, 2008

  7. This code does not work. It’s outdated now

    By desbest on Jun 23, 2008

  8. Your code can tell whether something is in a url format such as it beginning with http:// or http://www. but it does not say whether a url actually exists.

    i tried your code with a domain that does not exist and it said “1″ that the url was valid

    By desbest on Jun 23, 2008

  9. After 10 minutes searching for a better url validator than this one, I couldn’t find one. I’ve added you to delicious.

    Even the check if a url exist snippets dont work and require curl.

    By desbest on Jun 23, 2008

  10. hi, desbest, mine script is a simple checker that may still need perfection. if there’s anything you can contribute to perfect it, i would appreciate it, i would try to perfect it if i got more time. maybe for now you can take a look at the following link which might be the thing you are looking for:

    http://www.bootstrike.com/PHPAJAXDomainCheck/#bulkdomain

    By admin on Jun 23, 2008

  11. Not working… :(

    It allows garbage urls…

    By Marian Bucur on Aug 5, 2008

  12. Best Site good looking freexnxx >:-]]

    By Hawyqpyc on Sep 21, 2008

  13. Thanks funny site

    By Wawlaywa on Sep 27, 2008

Sorry, comments for this entry are closed at this time.