Azu Posted October 23, 2007 Share Posted October 23, 2007 Hello I'm working on making a regex for URL links since none of the ones I've found work right for me. I just have one little problem with it.. I can't figure out how to make it give http:// as the default protocol if there is no protocol given. Like with the URL www.google.com/search?q=Foo And since I don't know how to make it add one, clicking on the link ends up taking you to http://MyWebsite.com/www.google.com/search?q=Foo Instead of http://www.google.com/search?q=Foo So please tell me how to do this in regex.. In PHP I would do it somewhat like this if(substr($Blah,0,4)=='http')$Foo='http'; elseif(substr($Blah,0,3)=='ftp')$Foo='ftp'; //etc else$Foo='http'; Edit: the way I have it in regex right now is like (https?|ftp) Anyways ya basically I want that to return "http" if it doesn't match https? or ftp. Link to comment https://forums.phpfreaks.com/topic/74426-if-no-protocol/ Share on other sites More sharing options...
effigy Posted October 23, 2007 Share Posted October 23, 2007 I assume you're using preg_replace? Change to preg_replace_callback. Link to comment https://forums.phpfreaks.com/topic/74426-if-no-protocol/#findComment-376200 Share on other sites More sharing options...
Azu Posted October 23, 2007 Author Share Posted October 23, 2007 Thanks.. I was wandering how to do it in (non server-language dependant) regex though, not in PHP.. Link to comment https://forums.phpfreaks.com/topic/74426-if-no-protocol/#findComment-376232 Share on other sites More sharing options...
effigy Posted October 23, 2007 Share Posted October 23, 2007 This cannot be done unless you: 1. Always strip any existing protocols and add http://. 2. Run it through another regex. Link to comment https://forums.phpfreaks.com/topic/74426-if-no-protocol/#findComment-376259 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.