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
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);
?>

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.