detest Posted October 31, 2008 Share Posted October 31, 2008 I've come across some URL validation regexes and even a PHP function, but they all contain flaws! ex. the PHP function: filter_var($url, FILTER_VALIDATE_URL) This says the input "http://www.example.com/http://www.example.com/" is valid! ex. the regex: return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url); This also says the input "http://www.example.com/http://www.example.com/" is valid! So I would like perfect URL validation. Also, I would like to avoid "http://", so the inputted url "www.example.com" would be valid. Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/130927-solved-perfect-url-validation/ Share on other sites More sharing options...
Mchl Posted October 31, 2008 Share Posted October 31, 2008 Well... they are valid... sort of... http://en.wikipedia.org/wiki/Http://example.com Link to comment https://forums.phpfreaks.com/topic/130927-solved-perfect-url-validation/#findComment-679675 Share on other sites More sharing options...
detest Posted October 31, 2008 Author Share Posted October 31, 2008 Ah.. I never thought of that. How can I properly validate it without "http://" though? Link to comment https://forums.phpfreaks.com/topic/130927-solved-perfect-url-validation/#findComment-679680 Share on other sites More sharing options...
thebadbad Posted October 31, 2008 Share Posted October 31, 2008 To make a match optional in regular expressions, put it inside parentheses and add a question mark right after it: preg_match('|^(http(s)?://)?[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url); Link to comment https://forums.phpfreaks.com/topic/130927-solved-perfect-url-validation/#findComment-679722 Share on other sites More sharing options...
detest Posted October 31, 2008 Author Share Posted October 31, 2008 k thanks Link to comment https://forums.phpfreaks.com/topic/130927-solved-perfect-url-validation/#findComment-679729 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.