jasonc Posted May 9, 2010 Share Posted May 9, 2010 I wish to check to see if a string entered if formatted as a URL or not. http://www.site.com .co.uk .org .org.uk and the other TLD's also www.site.com or site.com what method should i use to check this. Link to comment https://forums.phpfreaks.com/topic/201167-test-for-valid-url-format-of-string/ Share on other sites More sharing options...
JAY6390 Posted May 9, 2010 Share Posted May 9, 2010 A regex or parse_url I'd imagine Link to comment https://forums.phpfreaks.com/topic/201167-test-for-valid-url-format-of-string/#findComment-1055442 Share on other sites More sharing options...
jasonc Posted May 9, 2010 Author Share Posted May 9, 2010 i just tried the <?php $url = 'http://site.com/test/test.html'; print_r(parse_url($url)); echo parse_url($url, PHP_URL_PATH); ?> shows the HOST as www.site.com but if they were to just put say... site.com/test/test.html then HOST is empty and the PATH shows the original string. how can i test to see if any version of the url was entered. Link to comment https://forums.phpfreaks.com/topic/201167-test-for-valid-url-format-of-string/#findComment-1055448 Share on other sites More sharing options...
jasonc Posted May 9, 2010 Author Share Posted May 9, 2010 if i say what i am wanting to do or prevent this may help a bit more... i wish to prevent a URL web link from being posted in my forum. so if they were to type in say 'check this out www.site.com' or 'check this out site.com its cool!' or what ever takes their fancy. i have had more recently a few web link to 'buy this software' or the like type of entries and do not wish to be monitoring every single post as this is just not practical with the 100's that are coming in. and asking visitors to sign up is a no-no as they will just not post the valid posts. just wanting to keep that happy medium and try to automate the site a bit more. Link to comment https://forums.phpfreaks.com/topic/201167-test-for-valid-url-format-of-string/#findComment-1055451 Share on other sites More sharing options...
jasonc Posted May 9, 2010 Author Share Posted May 9, 2010 thinking about this some more and i think this is what i am after.. to find out if the string has site.com in it, or what ever the domain is i know this may bring up some false positives but then they can just send them to us via the contact page if needed. Link to comment https://forums.phpfreaks.com/topic/201167-test-for-valid-url-format-of-string/#findComment-1055454 Share on other sites More sharing options...
darkfreaks Posted May 9, 2010 Share Posted May 9, 2010 http://www.urgentclick.com/scripts/url-validation.html Link to comment https://forums.phpfreaks.com/topic/201167-test-for-valid-url-format-of-string/#findComment-1055455 Share on other sites More sharing options...
jasonc Posted May 9, 2010 Author Share Posted May 9, 2010 yes this is something like what i am after it checks the whole string, but can this be modified so it check if the string contains a URL 'something here site.com something else here' also if the URL is at the start or and the end not just within the string. Link to comment https://forums.phpfreaks.com/topic/201167-test-for-valid-url-format-of-string/#findComment-1055466 Share on other sites More sharing options...
darkfreaks Posted May 10, 2010 Share Posted May 10, 2010 // Checks if string is a URL // @param string $url // @return bool function isURL($url = NULL) { if($url==NULL) return false; $protocol = '(http://|https://)'; $allowed = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)'; $regex = "^". $protocol . // must include the protocol '(' . $allowed . '{1,63}\.)+'. // 1 or several sub domains with a max of 63 chars '[a-z]' . '{2,6}'; // followed by a TLD if(eregi($regex, $url)==true) return true; else return false; Link to comment https://forums.phpfreaks.com/topic/201167-test-for-valid-url-format-of-string/#findComment-1055631 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.