greenday Posted January 10, 2008 Share Posted January 10, 2008 Hi, when i output a web address from a DB entry, it could have been entered as just www.domain.com, or http://www.domain.com. I am trying to find a way where I can check if the http:// exists, and if not add it to the begining. Having tried various ways, I cant get it to work -any help please? Here is my code before I have tried any ways to get it to work: Web: <a href="<?php echo $row_clubinfo['clubweb']; ?>" target="_blank"><?php echo $row_clubinfo['clubweb'] = str_replace('http://', '', $row_clubinfo['clubweb']); ?></a> You will see from the code I am removing the http:// from the visible text, but need to add it to the href link. Thanks! Link to comment https://forums.phpfreaks.com/topic/85446-solved-add-http-if-its-not-there/ Share on other sites More sharing options...
Ken2k7 Posted January 10, 2008 Share Posted January 10, 2008 Try this method: (suggest you define it as a function and just call it. Obviously if you do, use return and not echo) <?php if (preg_match("/http:\/\//i", $row_clubinfo['clubweb']) echo $row_clubinfo['clubweb']; else echo "http://".$row_clubinfo['clubweb']; ?> Link to comment https://forums.phpfreaks.com/topic/85446-solved-add-http-if-its-not-there/#findComment-435994 Share on other sites More sharing options...
trq Posted January 10, 2008 Share Posted January 10, 2008 preg_match is pretty expensive when its really not required. <?php echo (substr($row_clubinfo['clubweb'],0,6) == 'http://') ? $row_clubinfo['clubweb'] : 'http://' . $row_clubinfo['clubweb']; ?> Link to comment https://forums.phpfreaks.com/topic/85446-solved-add-http-if-its-not-there/#findComment-435997 Share on other sites More sharing options...
Ken2k7 Posted January 10, 2008 Share Posted January 10, 2008 What does "expensive" mean in this case? ??? Link to comment https://forums.phpfreaks.com/topic/85446-solved-add-http-if-its-not-there/#findComment-435999 Share on other sites More sharing options...
drummer101 Posted January 10, 2008 Share Posted January 10, 2008 <?php if(substr($string,0,7) != "http://"){ echo "http://" . $string; } else { echo $string; } ?> Link to comment https://forums.phpfreaks.com/topic/85446-solved-add-http-if-its-not-there/#findComment-436001 Share on other sites More sharing options...
trq Posted January 10, 2008 Share Posted January 10, 2008 What does "expensive" mean in this case? preg_match is known to be a fair bit slower than substr, and seeing as where looking for a fixed string, there really is no need to use a tool like regex which is designed to find patterns. Link to comment https://forums.phpfreaks.com/topic/85446-solved-add-http-if-its-not-there/#findComment-436003 Share on other sites More sharing options...
greenday Posted January 10, 2008 Author Share Posted January 10, 2008 awesome - thanks so much guys for the quick responses - case solved! Link to comment https://forums.phpfreaks.com/topic/85446-solved-add-http-if-its-not-there/#findComment-436018 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.