random_ Posted October 3, 2013 Share Posted October 3, 2013 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 https://forums.phpfreaks.com/topic/282683-how-to-check-if-url-is-valid-without-filter_validate_url/ Share on other sites More sharing options...
AbraCadaver Posted October 3, 2013 Share Posted October 3, 2013 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)) {} Link to comment https://forums.phpfreaks.com/topic/282683-how-to-check-if-url-is-valid-without-filter_validate_url/#findComment-1452434 Share on other sites More sharing options...
.josh Posted October 3, 2013 Share Posted October 3, 2013 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. Link to comment https://forums.phpfreaks.com/topic/282683-how-to-check-if-url-is-valid-without-filter_validate_url/#findComment-1452440 Share on other sites More sharing options...
random_ Posted October 3, 2013 Author Share Posted October 3, 2013 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 https://forums.phpfreaks.com/topic/282683-how-to-check-if-url-is-valid-without-filter_validate_url/#findComment-1452442 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.