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 Link to comment https://forums.phpfreaks.com/topic/167534-solved-check-for-a-word-and-insert-a-word-if-not-there/ 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://" Link to comment https://forums.phpfreaks.com/topic/167534-solved-check-for-a-word-and-insert-a-word-if-not-there/#findComment-883466 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. Link to comment https://forums.phpfreaks.com/topic/167534-solved-check-for-a-word-and-insert-a-word-if-not-there/#findComment-883491 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 Link to comment https://forums.phpfreaks.com/topic/167534-solved-check-for-a-word-and-insert-a-word-if-not-there/#findComment-883514 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. Link to comment https://forums.phpfreaks.com/topic/167534-solved-check-for-a-word-and-insert-a-word-if-not-there/#findComment-883521 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.