Decode UTF-8 URL in Perl

女生的网名这么多〃 提交于 2019-12-30 04:49:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!