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! Quote Link to comment 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']; ?> Quote Link to comment 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']; ?> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted January 10, 2008 Share Posted January 10, 2008 What does "expensive" mean in this case? ??? Quote Link to comment 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; } ?> Quote Link to comment 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. Quote Link to comment 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! 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.