Forum Moderators: coopster & phranque

Message Too Old, No Replies

Newbie substitute question

Changing a url to clickable link in messages

         

Mountain Man

1:34 am on Dec 2, 2001 (gmt 0)



Newbie Question

how can I replace any URL in a string with a clickable link? For example, replace [mypage.com...] with <a href="http://mypage.com/mypage.html">http://mypage.com/mypage.html</a>.

I tried substiturte, bui it misses many urls.

if ($message_body =~ /http:\/\/(.*?)\. /) {
$cc = $1;
if ($cc =~ /"$/) {
chop $cc;
}
}
$message_body =~ s/http:\/\/$1/<a href=\"http:\/\/$cc\">http:\/\/$cc<\/a>/g;

What am I doing wrong, and how would I correct this?

gethan

6:58 pm on Dec 2, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld Mountain Man.

The main problem with your code is this line...

[perl]$message_body =~ /http:\/\/(.*?)\. /[/perl]

The pattern will actually match url. not url - notice the . on the end. It can also be simplified a little further - to one line

[perl]$message_body =~ s!(http://.*?)(\s¦(\.\s))!<a href="$1">$1</a>$3/!g;[/perl]

There maybe the odd exception but most are caught.

Symbols other than / can be used as delimiters in regex for situations just like this - I used "!" in this case.

Good luck.

Mountain Man

8:33 pm on Dec 2, 2001 (gmt 0)



Thanks for the quick response, I appreciate your help. It looks good to me, but when I tested it on this string:

$message_body = "John Crichton's Farscape module is swallowed by a wormhole and spat out on the other [members.aol.com...] side of the universe -- in the middle of a pitched space battle. Taken on board Moya -- a huge bio-mechanoid living ship desperately trying to escape
[members.aol.com...] Peacekeeper captivity -- Crichton is confronted by alien life forms: Ka D'Argo, the fierce Luxan warrior; Rygel XVI, the slug like Dominar";

When I printed $message_body, none of the URLs were changed. Did it work when you tried it?

Key_Master

8:52 pm on Dec 2, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this:

$message_body =~ s,[\s]http://(.*?)[\s\.], <a target="_blank" href="http://$1">http://$1</a> ,gi;

Place it right under $message_body = "$FORM{'message_body'}"; so that the script will parse for the URL's first.

PS. I just noticed that WebmasterWorld includes the period in the URL.

Mountain Man

9:11 pm on Dec 2, 2001 (gmt 0)



This one works partly, but chops off everything after period. e.g.

[members.aol.com...]

becomes

[members...]

Key_Master

10:32 pm on Dec 2, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$message_body =~ s,[\s]http://(.*?)[\s], <a target="_blank" href="http://$1">http://$1</a> ,gi;
$message_body =~ s/\.">/">/g;

Here's a better way...

Try putting both of the above underneath $FORM('message_body');

gethan

9:27 am on Dec 3, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep - it worked fine - whats happening is the ¦ is coming out as the wrong symbol when copied and pasted from WebmasterWorld. Copy and paste - delete that character and replace with the one above \ on your keyboard (well least on mine it is).

[perl]
$message_body = "John Crichton's Farscape module is swallowed by a wormhole and spat out on the other [url...] side of the universe -- in the middle of a pitched space battle. Taken on board Moya -- a huge bio-mechanoid living ship desperately trying to escape [url...]
[url...] Peacekeeper captivity -- Crichton is confronted by alien life forms: Ka D'Argo, the fierce Luxan warrior; Rygel XVI, the slug like Dominar";
$message_body =~ s!(http://.*?)(\s¦(\.\s))!<a href="$1">$1</a>$3!g;
print $message_body;
[/perl]

Mountain Man

5:14 pm on Dec 3, 2001 (gmt 0)



Thank you my friend, that's exactly what was happening. Appreciate your help.