homepage Welcome to WebmasterWorld Guest from 54.226.43.155
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Add hyperlinks to non-linked URLs
String operations
Philarmon




msg:1299474
 6:42 pm on Jun 14, 2005 (gmt 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!

 

StupidScript




msg:1299475
 7:49 pm on Jun 14, 2005 (gmt 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.

Birdman




msg:1299476
 8:25 pm on Jun 14, 2005 (gmt 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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved