The Hack o blog

Reinventing the weel

Auto Clickable URL’s in PHP

Php logo with lower text

Image via Wikipedia

Here’s something that as a PHP programmer I have found useful when writing code for a website which often refers to sources using links… It automatically parses URL‘s into clickable anchors without needing to use any actual tags – you just write the URL as it looks and it will convert it for you. It’s great for people who don’t really know about such things as HTML or BBCode and I have found it useful so if your a programmer feel free to use it if you wish.

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

Then when you wish to refer to it use this:

$string = "Go to this URL -";

4 responses to “Auto Clickable URL’s in PHP

  1. x2i4eva February 15, 2010 at 10:12 PM

    BTW, Ignore the links in the function, WordPress puts them there automatically using a similar function to this I am guessing 😛

    See real life example for you there.

  2. kenny February 16, 2010 at 2:27 AM

    did you rap them in code tags ?

    • x2i4eva February 18, 2010 at 12:29 AM

      Yeah I did lol – oh well

      • HackOblog September 4, 2010 at 4:58 PM

        I fixed it new BBcode has been emailed to you.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: