Jump to content

How to check if URL is valid without FILTER_VALIDATE_URL?


random_
Go to solution Solved by AbraCadaver,

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.

Link to comment
Share on other sites

  • Solution

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)) {}
Edited by AbraCadaver
Link to comment
Share on other sites

 

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.