Welcome to WebmasterWorld Guest from 54.224.160.42

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Add hyperlinks to non-linked URLs

String operations

     
6:42 pm on Jun 14, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:July 23, 2003
posts:45
votes: 0


Hi!

I have some large parts of text with URLs in it (but without hyperlinks). I need to add links to all URLs but have no idea about how to do that, so i'm asking for your help in the hope that someone have done it already and can share the code with others or is just a PHP wizard ;)

So how can i detect an URL (like [whatever.com)...] in a text and add hyperlink tags to it so it looks like

< a href=http://www.whatever.com >http://www.whatever.com< /a >

Please help :)
Thank you!

7:49 pm on June 14, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 20, 2004
posts:1475
votes: 0


You're looking to do a "regular expression search and replace". There are several threads in this forum that describe this in various ways. Sorry I can't post a link or the code, but do a search and you'll find lots of stuff.
8:25 pm on June 14, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 22, 2002
posts:2546
votes: 0


<?php
$text = "some large parts of text with URLs in it (but without hyperlinks)";
$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
"<a href=\"\\0\">\\0</a>", $text);
?>

There ya go! That came directly from the PHP manual page for ereg_replace [us2.php.net].

Cheers