Welcome to WebmasterWorld Guest from 107.20.34.173

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Add hyperlinks to non-linked URLs

String operations

     

Philarmon

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

10+ Year Member



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!

StupidScript

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

WebmasterWorld Senior Member 10+ Year Member



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.

Birdman

8:25 pm on Jun 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?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

 

Featured Threads

Hot Threads This Week

Hot Threads This Month