簡體   English   中英

如何使用Perl將HTML文件轉換為UTF-8

[英]how to convert HTML file to UTF-8 using Perl

我在下面的位置有一個HTML文件

http://jmp.sh/WnRlO82

我想將上面的HTML文件轉換為UTF-8編碼格式,我在Perl中嘗試了以下方法

use strict;
use Cwd;
use HTML::Entities;
use HTML::Entities::Numbered;
use HTML::Strip;
use Encode;
use utf8;

my $parentPath = $ARGV[0];
#my $parentPath = getcwd()."/".$epubHTMLName;
opendir(FOLDER,$parentPath) || die "No such directory !";
my @readDir = readdir(FOLDER);
close FOLDER;

foreach(@readDir){
    #print $_."\n";
    my $fold = $_;
    my $match = lc($fold);
    my @dotcount;
    while ($match=~m/(html)/gi){
        push(@dotcount,$1);
    }
    my $totalcount = @dotcount+0;

    print $fold."\n";

    if ($fold ne "." && $fold ne ".." && $totalcount eq 1) {
        my $input = $parentPath."\\".$fold;
        my $output = getcwd()."/Output/".$fold;
        open(HTMLFILE, $input) || die "Check your File !";
        local $/ = undef;
        my $htmlcontent = <HTMLFILE>;
        $htmlcontent = decode_entities($htmlcontent);
        $htmlcontent =~s/\x{feff}//g;
        close HTMLFILE;

        open my $out, '>:encoding(UTF-8)', $output;
        print {$out} $htmlcontent;
        close $out;
    }
}

通過上述方法轉換后,我可以在Google Chrome以UTF-8格式看到,但我無法在FireFox看到它。 因此,需要一種在ChromeFirefox以UTF-8查看HTML文件的方法。

輸出文件供您參考: http//jmp.sh/tp5QHwG

您的輸出文件是正確的。

默認情況下,Firefox不會以UTF-8編碼顯示頁面。 見這個討論

只需在<head>部分添加以下內容即可明確告訴它。

<meta charset="UTF-8" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM