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.

Sushant Danekar on May 12th, 2009 at 3:19 pm #

Hi There,
This is nice script.
I want a script which is exactly opposite of this.
THe script should remove the LInk and just put it as a normal text link…

Is any idea regarding this requirement.

REgards
Sushant Danekar

AJ on May 12th, 2009 at 3:29 pm #

Hi Sushant,

You can remove all html tags and make it text using the strip_tags() php function. You can find documentation here: http://in.php.net/strip_tags

You can also provide the allowable tags if you would like to leave any tags.

I hope this helps.
AJ

miza on June 15th, 2009 at 7:43 pm #

Thanks for that function! Much appreciated. Unfortunately I stumbled upon a problem when other characters are involved … like ä Ö etc.
I’m still trying to get my headaround the regex used. ‘&’ should be included but ‘;’ isn’t?
Thanks a lot - Michael.

miza on June 15th, 2009 at 7:44 pm #

ok, this comment was supposed to show the HTML-tag for the umlauts, sorry!

temistokles on June 29th, 2009 at 3:52 pm #

If I understand correctly, this could be the solution:

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

willy on July 8th, 2009 at 9:37 am #

I am not a programmer nor a developer, But I can do somethings.

I have a social network for business professionals. In the Users section, the website: http://www.somesite.com does not appear clickable or active.

How can I make this happen?

This is what I presently have:

To display the website entered by the new or existing member. I wanted to make it active or clickable.

animeez on August 24th, 2009 at 8:45 pm #

this script works great

what if a user is not aware that the link can automatically parsed when putting a link when he input tags between the link?

in this case, is it possible to create a script that strip all tags inputted by user manually then after that the text link will automatically parsed

miza on September 17th, 2009 at 8:51 pm #

temistokles, thanks for helping!
I tried that but it appears that the parser doesn’t like the `”` after the second href=
> syntax error, unexpected ‘[’ in …

simply escaping in the manner of \\” doesn’t help.
Any idea?

Thanks a lot, Michael.

Sporsho on October 10th, 2009 at 12:38 am #

Nice…
Very much helpful.
Thanks

John Moriarty on October 22nd, 2009 at 1:59 am #

the code at the top doesn’t work if there’s a “‘” in the URL either - e.g. http://www.johnlewis.com/Men/Men’s+Knitwear/Men’s+Knitwear/Men’s+Knitwear/6286/ProductCategory.aspx. Any suggestions of alterations to make it work?

Tom on January 6th, 2010 at 4:44 am #

eregi_replace is depreciated, here’s a version using preg_replace:

function makeClickableLinks($text)
{

$text = html_entity_decode($text);
$text = ” “.$text;
$etxt = preg_replace(’/(((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i’,’$1‘, $txt);
$text = preg_replace(’/(((f|ht){1}tps:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i’,’$1‘, $txt);
$text = preg_replace(’/([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i’,'$1$2‘, $txt);
$text = preg_replace(’/([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})/i’, ‘$1‘, $txt);
return $text;
}

Tom on January 6th, 2010 at 4:46 am #

oops, change all “$txt” to “$text” in the above. (cursed find replace)

IMWD on February 4th, 2010 at 9:54 pm #

Is there a way to implement this for use with a variable? I’ve got a table that draws an email from the database but displays it as text. If I do this:

echo “makeClickableLinks $line[2]“;

it displays this:

makeClickableLinks somebody@email.com

rather than as a link.

Any ideas? Many thanks.

IMWD on February 4th, 2010 at 10:42 pm #

In my posting above, the sample email was rendered as a link. It is not so on the web page. It is just plain text.

Post a comment
Name: 
Email: 
URL: 
Comments: