Detecting URL and Making it as link in Text

Sunday, July 17, 2011 4:20
Posted in category PHP

We might be having large text, with urls without links. Instead of editing the text or post, we can make the url as link dynamically while displaying it in browser.

<?php
$text = 'This is a simple text to find http://www.google.com
and http://www.about.com urls from the text and make it as links
URLs will be dynamically formatted as links, this avoid editing each post or text
in large archives or database - http://www.largeresource.net/ ';
$text = preg_replace( "/(^|\s)((http|https|ftp):\/\/[^\s<]+)/i", '\\1<a href="\\2">\\2</a>', $text );
echo $text;
?>

The above code will find the urls in $text and make it as links dynamically. This code can be used in any part of code structure easily.

You can leave a response, or trackback from your own site.

One Response to “Detecting URL and Making it as link in Text”

  1. Rex Gilley says:

    July 19th, 2011 at 6:35 pm

    Very useful info. Hope to see more posts soon!

Leave a Reply

*