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
https://forums.phpfreaks.com/topic/85446-solved-add-http-if-its-not-there/
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'];
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.