Forum Moderators: coopster

Message Too Old, No Replies

Auto Linking with PHP

How to get make_clickable to work on my pages

         

kokaroach

8:12 pm on Apr 25, 2005 (gmt 0)

10+ Year Member



Hi all,

I have a bunch of articles with plain text url's. I'll be plugging them into templates and rather than type in the a href= yada yada, I'd like to use php to automatically convert those url's clickable hyperlinks whenever someone visits the page.

Can that be done with the following function?

Of so, how the heck do I get it to work on my pages?

<?php

function make_clickable($text) {
$ret = " " . $text;
$ret = preg_replace("#([\n ])([a-z]+?)://([^, <>{}\n\r]+)#i", "\\1<a href=\"\\2://\\3\" target=\"_blank\">\\2://\\3</a>", $ret);
$ret = preg_replace("#([\n ])www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[^,< \n\r]*)?)#i", "\\1<a href=\"http://www.\\2.\\3\\4\" target=\"_blank\">www.\\2.\\3\\4</a>", $ret);
$ret = substr($ret, 1);
return $ret;
}

?>

Any help would be appreciated enormously!

Thanks in advance,

kokaroach

buriedUnderGround

7:56 am on Apr 26, 2005 (gmt 0)

10+ Year Member



This should be quite straightforward...

<?
//your function here

//get your plain text from your text files
$myText = file_get_contents($fileLocation);

//and echo the result of the function applied to it.
echo make_clickable($myText);
?>

NOTE:
from [php.net...]
In earlier versions than 4.3.0 you can use this for the 'file_get_contents' function:
<?php
if(!function_exists('file_get_contents')) {
function file_get_contents($file) {
$file = file($file);
return!$file? false : implode('', $file);
}
}
?>