[英]Perl: Convert (high) decimal NCR to UTF-8
我有這個字符串(十進制NCR): 日本の鍼灸とは
它代表日本文本日本の鍼灸とは
。
但我需要(UTF-8): %E6%97%A5%E6%9C%AC%E3%81%AE%E9%8D%BC%E7%81%B8%E3%81%A8%E3%81%AF
對於第一個角色: 日
⇒ 日
⇒ %E6%97%A5
這個網站做到了,但我如何在Perl中獲得這個? (如果可能在單個正則表達式中,如s/\\&\\#([0-9]+);/uc('%'.unpack("H2", pack("c", $1)))/eg;
)
http://www.endmemo.com/unicode/unicodeconverter.php
此外,我需要將其從UTF-8再次轉換回十進制NCR
我現在已經半天打破了這一天,任何幫助都非常感謝!
您所謂的“UTF-8”實際上是URL編碼。
HTML實體( 日
)⇒文本( 日
) 日
組件( %E6%97%A5
):
use HTML::Entities qw( decode_entities );
use URI::Escape qw( uri_escape_utf8 );
my $text = decode_entities($html);
my $uri_component = uri_escape_utf8($text);
URI組件( %E6%97%A5
)⇒文本( 日
) 日
實體( 日
):
use Encode qw( decode_utf8 );
use HTML::Entities qw( encode_entities );
use URI::Escape qw( uri_unescape );
my $text = decode_utf8(uri_unescape($uri_component));
my $html = encode_entities($text);
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 2;
use Encode qw{ encode decode };
my $in = '日本の鍼灸とは'; # 日本の鍼灸とは
my $out = '%E6%97%A5%E6%9C%AC%E3%81%AE%E9%8D%BC%E7%81%B8%E3%81%A8%E3%81%AF';
(my $utf = $in) =~ s/&#(.*?);/chr $1/ge;
my $r = join q(), map { sprintf '%%%2X', ord } split //, encode('utf8', $utf);
is($r, $out);
(my $s = $r) =~ s/%(..)/chr hex $1/ge;
$s = decode('utf8', $s);
$s = join q(), map '&#' . ord . ';', split //, $s;
is($s, $in);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.