Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Make URL's clickable

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Make URL's clickable

Postby Neil Hillman » Mon Jul 14, 2003 4:07 am

I have built a basic guestbook in PHP and I want to dynamically display URLs as clickable A HREF links, so what I'm looking for is the erigi expression to turn:
Code: Select all
Check out http://www.mylink.com/link.html

- into -
Code: Select all
Check out <A HREF="http://www.mylink.com/link.html">http://www.mylink.com/link.html</A>

I would be v. grateful if any of you PHP gurus or RegEx geniuses could tell me the code I should be using because I suck at regular expressions!
Thanks
Neil Hillman
New php-forum User
New php-forum User
 
Posts: 4
Joined: Mon Jul 14, 2003 3:58 am
Location: Geneva, Switz.

Postby Neil Hillman » Mon Jul 14, 2003 4:46 am

Something like:

Code: Select all
$link = ereg("[[:alpha:]]+://[^<>[:space:]]+[[:alum:]/]");
foreach($link as $links) {
    echo "<A HREF=\"$links\">$links</A>";   
}


except;
A) I don't think this code is tight enough, it may match too widely, and...
B) I want to alter the contents of the original $string, not output the results like this does...

Please help!
Neil Hillman
New php-forum User
New php-forum User
 
Posts: 4
Joined: Mon Jul 14, 2003 3:58 am
Location: Geneva, Switz.

Postby Neil Hillman » Mon Jul 14, 2003 5:55 am

Okay, I figured this out if anybody's interested...
Code: Select all
$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $text);

Seems to do the trick...
Neil Hillman
New php-forum User
New php-forum User
 
Posts: 4
Joined: Mon Jul 14, 2003 3:58 am
Location: Geneva, Switz.


Return to PHP coding => General

Who is online

Users browsing this forum: Google Feedfetcher and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron