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?
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.
$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?
[members.aol.com...]
becomes
[members...]
[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]