Make a link of an URL in a sentence

Top Japanese page




Overview

Automatically link to the sentenses starting from http://.

Flow

  1. Check patterns and convert the characters

A sample code

 my $url_pattern = 'https?:\/\/[\w\.~\/\?&\+=:@%;#\$%,\-]*';
 
 $text =~ s/($url_pattern)/<a href=\"$1\">$1<\/a>/g;

Description of the code

 my $url_pattern = 'https?:\/\/[\w\.~\/\?&\+=:@%;#\$%,\-]*';

Put the pattern to be searched into a variable. This process is not absolutely necessary. If the pattern match occurs multiple times in the script, it is recommended ot do just for convenience.

 $text =~ s/($url_pattern)/<a href=\"$1\">$1<\/a>/g;

Replace URL patterns in $text by s///g. The pattern is used in <a href> tag and in between <a></a>.