Jump to content

Website URL conversion


mmsolutions

Recommended Posts

This is a bit of a niche really.

 

What I want is when you type the following URL ( http://www.google.co.uk ) and it gets saved into the mysql database.

 

Next step is when this is echoed onto the website I want this to be displayed as <a href="http://www.google.co.uk">http://www.google.co.uk</a> so that users can click on the link as a live hyperlink.

 

This is often acheived on most blogs and it must be relatively simple.

 

Many thanks

Link to comment
https://forums.phpfreaks.com/topic/182769-website-url-conversion/
Share on other sites

If the database field simply contains the URL, it's just as simple as

 

<?php
//variable $url retrieved from database call
echo "<a href=\"$url\">$url</a>";
?>

 

If you want 'inline' URLs to be converted, e.g. in a body text, you can use a fairly simple regular expression:

 

<?php
$text = 'Inline URLs like http://google.com/ found in this string will be converted to HTML links.';
echo preg_replace('~\bhttps?://\S+~i', '<a href="$0">$0</a>', $text);
?>

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.