Helping ordinary people create extraordinary websites!
   

How to turn URLs into links using PHP?

Tuesday, 3rd November 2009
by Emily

What code can I use to change a link eg. http://www.mysite.com or www.mysite.com into a live clickable link on my webpage?


Tags:

Vader
Hi Emily this function should achieve what you need done.

function auto_link($content){
$string = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\" target=\"_blank\">\\0</a>", $content);
return $string;
}

echo auto_link($content);

Where content is any page content that you want the urls to become links. Also note that this code will only auto link urls with the http:// so if you simply had www.domain.com it wouldn't link it. This can be particularly useful if you will have URLs on the page that you do not want to be linked.

Vader.
Monday, 9th November 2009
Votes:
36
10

More PHP Help:
» How to limit the number of words in my display with PHP?
» How to get database values in table format on form?
» Code to ask a user to enter data for creating a table using ajax and php
» How to run PHP on my desktop computer?
» How can I make a PHP page reload continually forever?
» How to select and display a set of images from a folder using PHP?
» Convert a string into an array
» How i'll send mail using php script from localhost?