Forum Moderators: coopster & phranque

Message Too Old, No Replies

Replacing URLs in strings

         

steekyjim

10:24 pm on Sep 5, 2005 (gmt 0)

10+ Year Member



I'm writing a message board. The peole who use my board don't know html. When someone makes a post I want it to automatically convert any urls given in their post into an html hyperlink eg. <a href="http://whatever.com" target="black">http://www.whatever.com</a>

My string is called $in{'message'} and it would contain something like this.....

$in{'message'} = "hello... a great website for that is [whatever.com...] ";

I want it to detect urls in the string and convert them to the necessary html.

Looked everywhere and haven't found anything :-(

Please help me. Thanks in advance, John

steekyjim

10:25 pm on Sep 5, 2005 (gmt 0)

10+ Year Member



see this page did it with my link! grrrr.... That's what i want it to do.

KevinADC

12:49 am on Sep 6, 2005 (gmt 0)

10+ Year Member



this will work for most cases, but not all:


my $message = qq~This is a great site [site.com...] and I love this one too www.othersite.com and also [secure.com...] and ftp://myftp.com~;
$message =~ s/(https?¦ftp)(:\/\/)?([\w.-]+)/<a href="$1:\/\/$3">$1:\/\/$3<\/a>/ig;
print $message

note that www.othersite.com does not get converted into a hyper-link with the above code. And not that this message board does not convert links that begin with https and ftp into hyper-links.

[edited by: coopster at 4:39 pm (utc) on Sep. 7, 2005]
[edit reason] fixed sidescroll [/edit]

TheWhippinpost

12:54 am on Sep 6, 2005 (gmt 0)

10+ Year Member



Try using URI::Find

[Sorry, didn't see Kev's post]

steekyjim

1:36 am on Sep 7, 2005 (gmt 0)

10+ Year Member



I tried that code as it was....

It just printed out the string and didn't converted it.

(I did have to put a semi-colon on the end of the print line but it stiill didn't work)

I dont have a baldy how the code in a line like that works so I'd be glad if you can help. While I'm waiting I'll try the ULR::FIND thingy.

Thanks a lot if you can help

John

BananaFish

3:55 am on Sep 8, 2005 (gmt 0)

10+ Year Member



This will do the trick:

$in{'message'}=~s/(http(s)*:\/\/)*www\.([a-z0-9\._\/-]+)\.([a-z]{2,5})/<a href=\"http$2:\/\/www.$3.$4\">$1www.$3.$4<\/a>/ig;

KevinADC

4:44 pm on Sep 8, 2005 (gmt 0)

10+ Year Member




I tried that code as it was....

It just printed out the string and didn't converted it.

(I did have to put a semi-colon on the end of the print line but it stiill didn't work)

I dont have a baldy how the code in a line like that works so I'd be glad if you can help. While I'm waiting I'll try the ULR::FIND thingy.

Thanks a lot if you can help

John

post what you tried and the string. The code I posted works, so I suspect a problem elsewhere in your code.