Jump to content

How to check if URL is valid without FILTER_VALIDATE_URL?


random_

Recommended Posts

Hello guys, I am new at this forum. Tought it was right placeto ask this question. So I was using php function:

 filter_var($url, FILTER_VALIDATE_URL)

but it onliy accepts input in format with protocol specified e.g. http://

 

I need to be able to check if url is in formats like this:

http://www.example.com

http://example.com

www.example.com

example.com

 

I googled around and I couldn't find anything useful, tough this script was something not that bad but yet still not usefull cause it accepts domain like ".com" which is invalid. Here is the script:

<?php 
    $regex = "((https?|ftp)\:\/\/)?"; // SCHEME 
    $regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass 
    $regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP 
    $regex .= "(\:[0-9]{2,5})?"; // Port 
    $regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path 
    $regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query 
    $regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor 

	$url = ".com";
	
       if(preg_match("/^$regex$/", $url)) 
       { 
            echo "Valid";
       } 
?>

Maybe this script can be improved to with some string validation e.g. Lenght of URL $domain.$tld can not be smaller than 5 char if $tld is 3 char long and URL can not be smaller than 4 char if $tld is 2 char long. But what about domains wher tld is like this - .co.uk?

 

So my question is does anyone have such script that can share?

 

Thanks.

www.example.com is not a URL, it's a fully qualified domain name.  A URL contains the protocol as well.  Why not check and if not, add a default one?  Something like:

if(strpos($url, "://") === false) { $url = "http://$url"; }

Or use a preg_match() to be stricter.  Then use filter_var($url, FILTER_VALIDATE_URL).

 

Or maybe:

if(!parse_url($url, PHP_URL_SCHEME)) {}

yeah...sounds like the problem here is that your definition of what a URL is and what the real definition of a URL is are 2 different things. If you want to go by your definition, AbraCadaver makes some good suggestions, though parse_url does have certain limits with its ability to parse. this comment offers a better parser.

 

www.example.com is not a URL, it's a fully qualified domain name.  A URL contains the protocol as well.  Why not check and if not, add a default one?  Something like:

if(strpos($url, "://") === false) { $url = "http://$url"; }

Or use a preg_match() to be stricter.  Then use filter_var($url, FILTER_VALIDATE_URL).

 

Or maybe:

if(!parse_url($url, PHP_URL_SCHEME)) {}

 

First part works for me, at least for now. I forgot to mention that already tried parse_url and like Josh mentioned it has some limitation so i didnt found it useful. Thanks.

 

@Josh

Thanks, I will look into that example latter, for now its to advanced for me, tbh I have never had a situation for using regular expresions so I didnt learned them. Yeah, I'm sort of novice :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.