« Previous entry | Next entry » Browse > Snippets

Skip to comments (20) PHP: Automatic Hyperlink Function
Posted by dustin on Oct 16 2005 @ 13:56  :: 5681 unique visits

It took my awhile to get this all right, and it's a huge timesaver if you're looking to make a message board/shout box/some sort of system where users can just post an email or URL (http/https/ftp) in plain text, then have your code automatically create the hyperlink.

CODE: PHP
function autolink($foo) {
 $foo = preg_replace('/(?<!S)([w.]+)(@)([w.]+)b/i', '<a href="mailto:$1@$3">$1@$3</a>', $foo);
 $foo = preg_replace('/(?<!S)((http(s?)://)|(www.))+([w./&=#?-~%;]+)b/i', '<a href="http$3://$4$5" target="_blank">http$3://$4$5</a>', $foo);
 $foo = preg_replace('/(?<!S)((ftp(7?)://)|(ftp.))([w./&=#?-~%;]+)b/i', '<a href="ftp$3://$4$5" target="_blank">ftp$3://$4$5</a>', $foo);
 return ($foo);
}

20 comments posted so far
Add your own »

1. On Oct 20 2005 @ 20:48 guest wrote:

fine pice of code

2. On Jan 30 2006 @ 04:16 guest wrote:

wow.  nice.

3. On Jan 31 2006 @ 23:53 guest wrote:

Does it work if it the spelling would be http://www.test.com , www.test.com and   test.com?

4. On May 02 2006 @ 21:30 dustin wrote:

I just refound this site :P

to the above: it works on the first two, but not that last one, it's too common.

5. On May 13 2006 @ 23:10 guest wrote:

dont work :(
warning: preg_replace() [function.preg-replace]: Unknown modifier '/' in ...

6. On Jun 26 2006 @ 21:46 guest wrote:

yea i got that too....
Warning: preg_replace(): Unknown modifier '/' in....

7. On Jul 05 2006 @ 15:09 guest wrote:

Tried using it, didnt work. I might have dont something wrong but it erased my whole string.

8. On Jul 05 2006 @ 15:10 guest wrote:

done*

9. On Mar 26 2008 @ 18:46 guest wrote:

anyone got a solution? I got the "Warning: preg_replace(): Unknown modifier '/' in...." too.

10. On Oct 15 2008 @ 06:32 guest wrote:

Its missing the escaping of the /, try this:

function autolink($foo) {
$foo = preg_replace('/(?<!S)([w.]+)(@)([w.]+)b/i', '<a href="mailto:$1@$3">$1@$3</a>', $foo);
$foo = preg_replace('/(?<!S)((http(s?):\/\/)|(www.))+([w.\/&=#?-~%;]+)b/i', '<a href="http$3://$4$5" target="_blank">http$3://$4$5</a>', $foo);
$foo = preg_replace('/(?<!S)((ftp(7?):\/\/)|(ftp.))([w.\/&=#?-~%;]+)b/i', '<a href="ftp$3://$4$5" target="_blank">ftp$3://$4$5</a>', $foo);
return ($foo);
}

11. On Oct 15 2008 @ 06:34 guest wrote:

This is a handy function, and works pretty well, except when there is a trailing / ie:

http://www.google.com/adsense/ it will return a href link of http://www.google.com/adsense

12. On Oct 22 2008 @ 18:16 guest wrote:

I tried the modified code is post #11.  I doesn't give the error like the previous version, but it seems ineffective.  I put together some test code...

echo("Test 1...<br>");
$testString = "www.aerialvisuals.ca";
echo("Before:  " . $testString . "<br>");
echo("After:  " . autolink($testString) . "<br><br>");

echo("Test 2...<br>");
$testString = "http://www.aerialvisuals.ca";
echo("Before:  " . $testString . "<br>");
echo("After:  " . autolink($testString) . "<br><br>");

echo("Test 3...<br>");
$testString = "http://www.aerialvisuals.ca/Locator.php";
echo("Before:  " . $testString . "<br>");
echo("After:  " . autolink($testString) . "<br><br>");

echo("Test 4...<br>");
$testString = "http://www.aerialvisuals.ca/Locator.php?Serial=1425";
echo("Before:  " . $testString . "<br>");
echo("After:  " . autolink($testString) . "<br><br>");

...on this page...

http://www.aerialvisuals.ca/Admin/TestPage_PHP_Stuff.php

...so you can check it out for yourself.  As you can see there is no effect.  Am I missing something here?

Mike

13. On Oct 22 2008 @ 21:23 Mike wrote:

I found something that works a bit better here...
http://www.totallyphp.co.uk/code/convert_links_into_clickable_hyperlinks.htm

I found it could handle "http://www.aerialvisuals.ca" but could not handle "www.aerialvisuals.ca".  I modified the function to...

function autolink_test($foo)
{
// Modified from:  http://www.totallyphp.co.uk/code/convert_links_into_clickable_hyperlinks.htm
$foo = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $foo);
if( strpos($foo, "http") === FALSE )
{
$foo = eregi_replace('(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="http://\\1" target=_blank rel=nofollow >\\1</a>', $foo);
}
else
{
$foo = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="http://\\2" target=_blank rel=nofollow >\\2</a>', $foo);
}
$foo = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', '<a href="mailto:\\1">\\1</a>', $foo);
return $foo;
}

...which allowed me to edit a minimal of regular expresses.  I don't really understand regular expressions anyway.  Unfortunately I am not able to get the link to open in a new window.  Here is a page where I am trying to test the function...

http://www.aerialvisuals.ca/Admin/TestPage_PHP_Stuff2.php

Thoughts anyone?

Mike

15. On Apr 13 2009 @ 14:14 Valencia wrote:

Hi, I tried this piece of code there was a warning as follows:

Warning: preg_replace(): Unknown modifier '/' in....

I have no time for working onto this because I am busy in my 642-812 Exam preparation. So anyone can help me about the warning. I have also plan of giving 642-901. I have a friend who is a PHP programmer but she can’t help me because of her 70-291 Exam. So I need any body’s help in this matter. I badly need this piece of code but it should be correct and working. In future, there would be no free time for me because of 220-601 Exam preparation.

16. On Apr 29 2009 @ 11:38 guest wrote:

When the wolf wow gold finally found the hole Buy Wow Gold in the chimney he Cheap WoW Gold crawled down and KERSPLASH right into that kettle of water cheapest wow gold and that was the end of his troubles with the big bad wolf.

game4power,buy cheap wow gold
WOW GOLD

The next day the wow gold cheap little pig invited his mother over . She said "You see it is just as buy gold wowI told you. The way to Wow Goldget along in the world is to do things as well as you can." Fortunately for that world of warcraft gold little pig, he Cheapest wow Goldlearned that lesson. And he just lived happily ever after!

17. On May 25 2009 @ 08:10 maryzheng wrote:

We have been producing hydraulic tools, hydraulic crimping tool and other kinds of crimping tools.We can provide our customer good quality ,pvc windows machine,Wholesale PVC window hardwares, PE pipe fittings and other pvc prod\
ugg boots, world wild ugg boots cheap ugg boots, ugg boot, cheap ugg boot, ugg cardy, ugg cardy for uk, cheap ugg cardy boots are popular,we sell real ugg boots, cheap ugg boots, ugg cardy, cheap ugg cardy boots,ugg boots uk at great prices to people, we are proud to offer you the high quality and low price ugg classic cardy,ugg classic cardy shop, cardy ugg boots, cheap cardy ugg boots, ugg classic cardy boots, ugg classic cardy boots online, and ugg classic tall, ugg classic tall boot, ugg boots uk, cardy ugg boots are using 100% natural sheepskin

19. On Jul 14 2009 @ 04:01 guest wrote:

buy wow gold
my wow power leveling
buy wow gold
good wow power leveling
BUY wow gold
my wow power leveling
CHEAP rs gold
cheap wow power leveling
CHEAPEST lotro gold
MY aion gold
buy wow gold
cheap wow gold
CHEAPEST wow gold

Add a new comment

Name:
Password: (leave empty for anonymous comment)
 
View formatting tags Comment: