lynxus Posted July 26, 2009 Share Posted July 26, 2009 Hi guys, Probably a simple answer, but not having much luck here. Ive got a bunch of websites in a DB, however some dont start with http:// So when i run an output to a php file, some links dont work as im doing echo '<a href="'.$website.'">'.$website.'</a> Anyway. Let say: $var = "google.com"; how do i look at $var see that it doesnt start with http:// and then change it so: $var = "http://google.com"; Any thoughts? Thanks G Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 26, 2009 Share Posted July 26, 2009 You can use the substr function to do what you need. Select the first 7 characters from the string and check if it is equal to "http://" Quote Link to comment Share on other sites More sharing options...
vineld Posted July 26, 2009 Share Posted July 26, 2009 That will do. If this is part of your own application you should however see to that all values are correct when inserted instead of ending up with this duct tape solution. Every little thing that speeds up your applications will save time, money and the environment. Quote Link to comment Share on other sites More sharing options...
thewooleymammoth Posted July 26, 2009 Share Posted July 26, 2009 you could also use reg ex if its not necessarily the first 7 chars or you could use stripos() http://us3.php.net/manual/en/function.stripos.php Quote Link to comment Share on other sites More sharing options...
ldougherty Posted July 26, 2009 Share Posted July 26, 2009 I'd definitely use the substr reference to accomplish what you need now. if (substr('$var',0,4) != 'http') { $var = 'http://' . $var; } But as vineld said you should do error checking on your form input to determine if it starts with http:// or not and add it there instead. 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.