I am developing a website that is heavy on domain names, so a good URL validation function is really important. I have searched a lot but haven't been able to find one that seems to work very well.
Can anybody help me out?
The views expressed on this page by users and staff are their own, not those of NamePros.
[FONT="myriad pro,helvetica,Arial"]Are you just verifying the URL exists? If so, you can do that by grabbing the headers. Try:
PHP:
function url_exists($url){
@$headers=get_headers($url);
return preg_match('/^HTTP\/\d\.\d\s+(200|301|302)/',$headers[0]);
}
It will return true as long as the URL returns 200 (ok), 301 (permanently redirected), or 302 (temporarily redirected). get_headers is a PHP 5 function.[/FONT]
if you're trying to validate that the text input by a user really is a url string, you could do something like filter_var($url, FILTER_VALIDATE_URL); assuming you hvae php5