目次 †
NCRとは? †
- 数値文字参照(Numeric Character Reference)のこと
JavaScript実装例 †
NKFでNCRから文字列へ変換 †
太田さんがコンパイルしてくれたソフト(ネイティブから) †
Perlでの実装例 †
#!/usr/bin/perl -w
### 初期設定
use strict;
use Encode qw(encode decode :fallbacks);
### 内部コードに変換(必須)
my $string = "テストの文章";
my $internal = decode('utf8',$string);
### 変換結果を出力
my $result = encode('ascii',$internal,FB_HTMLCREF);
print $result."\n";
### 終了
exit;
Mattがくれたスクリプト †
#!/usr/bin/perl
#===============================================================
# Modules to be used
#===============================================================
{ no warnings; require v5.8.0; }
use strict;
use warnings;
use utf8;
use Encode qw(decode encode from_to);
#===============================================================
# Main
#===============================================================
my $file = $ARGV[0];
open(IN,"<$file") or die "Failed opening file: $file";
while(<IN>){
chomp;
print translate($_)."\n";
}
exit(0);
#===============================================================
# Sub routines
#===============================================================
sub dec2hex {
my $dec = shift;
return "" unless (defined $dec and length $dec);
my $hex = sprintf "%1x", $dec;
return uc($hex);
}
sub translate {
my $name = shift;
chomp $name;
$name =~ s/^\s*//;
$name =~ s/\s*$//;
if ($name =~ /^<font/) {
$name =~ s/^<font .*?>//;
$name =~ s/<\/font>$//;
}
$name =~ s/^\s*//;
$name =~ s/\s*$//;
my $string = "";
utf8::upgrade($string);
my @chunks = split(';',$name);
foreach my $chunk (@chunks) {
if ($chunk =~ /&#/) {
my ($prefix,$part) = split('&#',$chunk,2);
$string .= $prefix if (defined $prefix);
$ncr = dec2hex($ncr);
my $chr = "";
eval '$chr = "\x{'.$ncr.'}";';
$string .= $chr;
} else {
$string .= $chunk;
}
}
return $string;
}