Jump to content

[SOLVED] Add http:// if its not there


greenday

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.