15 March 2005
This post may be outdated due to it was written on 2005. The links may be broken. The code may be not working anymore. Leave comments if needed.
转为 utf-8 是为国际接轨。 :)
Download here!

Code

#!/usr/bin/perl
# convert gb2312 encoding webpage to utf-8 in a directory
use strict;
use warnings;
use Encode qw/from_to/; # load the main func.

# setting
my $dir = 'E:/Fayland/Emag/0503'; # the directory u want to convert.

# get all .html? files
opendir(DIR, $dir);
my @file = readdir(DIR);
closedir(DIR);
@file = grep(/\.html?$/, @file);

# convertion
foreach (@file) {
    # get the file data;
    open(FH, "$dir/$_");
    my @data = ;
    close(FH);
    my $data = join("", @data);
    if ($data =~ /charset\=gb2312/) { # it's not utf-8 yet
        $data =~ s/charset\=gb2312/charset\=utf-8/s;
        from_to($data, "gb2312", "utf8");    
        open(FH, ">$dir/$_");
        print FH $data;
        close(FH);
        print "$_ convert success!\n";
    } else {
        print "$_ is already utf-8\n";
    }
}


blog comments powered by Disqus