Filed Under (PHP Code Samples) by Anoop on May-26-2007
Code (php)

<?
function makeClickableLinks($text)
{

        $text = html_entity_decode($text);
        $text = " ".$text;
        $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
                '<a href="\\1" target=_blank>\\1</a>', $text);
        $text = eregi_replace('(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
                '<a href="\\1" target=_blank>\\1</a>', $text);
        $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
        '\\1<a href="http://\\2" target=_blank>\\2</a>', $text);
        $text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
        '<a href="mailto:\\1" target=_blank>\\1</a>', $text);
        return $text;
}

// Example Usage
echo makeClickableLinks("This is a test clickable link: http://www.websewak.com  You can also try using an email address like test@websewak.com");
?>
 


Gera on August 27th, 2007 at 3:02 am #

Hello. I need some help regarding this topic (converting text to html, with links, using php).

I have the following code that I have been working on. The problem is that this codes, inputs after every link in the text file, a
$html = nl2br(htmlspecialchars($data));

// replace multiple spaces with single spaces
$html = preg_replace(’/\s\s+/’, ‘ ‘, $html);

// replace URLs with elements
$html = preg_replace(’/\s(\w+:\/\/)(\S+)/’, ‘
\\1\\2‘, $html);

// start building output page
// add page header
$output =

.slug {font-size: 13pt; font-weight: bold}
.byline { font-style: normal }

HEADER;

// add page content
$output .= “$slug”;
$output .= “By $byline”;
$output .= “$html”;

// add page footer
$output .=

FOOTER;

// display in browser
echo $output;

// AND/OR

// write output to a new .html file
file_put_contents(basename($source, substr($source, strpos($source, ‘.’))) . “.html”, $output) or die(”Cannot write file”);
?>

Gera on August 27th, 2007 at 3:04 am #

Sorry It got cut up.

I have the following code that I have been working on. The problem is that this codes, inputs after every link in the text file, the code adds a

Gera on August 27th, 2007 at 8:48 am #

Again, arrr!
The code inputs the url with break (br /)

brian on September 18th, 2007 at 2:16 am #

Thanks! this is exactly what I was looking for. There’s another one floating around that doesn’t work with simply “www.google.com”, it makes you type the http:// great job!

ZeroCool on February 17th, 2008 at 7:34 pm #

Thanks man, it work’s perfectly!

Yari on March 12th, 2008 at 3:40 am #

Thats a great script Anoop, i’ve modified it slightly to make sure it doesn’t convert EXISTING links, such as link.

I’ve done this by adding a negate character class ([^]) before the main match:

// http
$text = eregi_replace(’(^((f|ht){1}tp://)[-a-zA-Z0-9@:%_+.~#?&//=]+)’,
\1‘, $text);

// https
$text = eregi_replace(’(^((f|ht){1}tps://)[-a-zA-Z0-9@:%_+.~#?&//=]+)’,
\1‘, $text);

// www
$text = eregi_replace(’([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_+.~#?&//=]+)’,
‘\1\2‘, $text);

however, I couldn’t get this to work with converting emails… any ideas?

Yari

Temistokles on June 11th, 2008 at 3:51 pm #

Yari:

I think, this could work.

$text = eregi_replace(’[^(href=|href=")](((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)’,
\\1‘, $text);
$text = eregi_replace(’[^(href=|href=")](((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)’,
\\1‘, $text);
$text = eregi_replace(’[^(href=|href=")]([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)’,
‘\\1\\2‘, $text);
$text = eregi_replace(’[^(href=|href=")]([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})’,
\\1‘, $text);

Chris on December 22nd, 2008 at 4:25 am #

Thanks!! Works fine.

Post a comment
Name: 
Email: 
URL: 
Comments: