问题
Consider:
use URI::Escape;
print uri_unescape("%C3%B3");
Output : ó
Decode with this http://meyerweb.com/eric/tools/dencoder/
Output : ó
This is the expected one.
What Perl library should I use to get the correct output?
回答1:
If you know that the byte sequence is UTF-8, then use Encode::decode
:
use Encode;
use URI::Escape;
my $in = "%C3%B3";
my $text = Encode::decode('utf8', uri_unescape($in));
print length($text); # Should print 1
回答2:
The code Encode::decode('utf8', uri_unescape($in))
doesn't work for me, but the following code works well.
sub smartdecode {
use URI::Escape qw( uri_unescape );
use utf8;
my $x = my $y = uri_unescape($_[0]);
return $x if utf8::decode($x);
return $y;
}
This code is from http://lwp.interglacial.com/ch05_02.htm
来源:https://stackoverflow.com/questions/13163937/decode-utf-8-url-in-perl