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. Quote Link to comment 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. Quote Link to comment 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.. Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.