Perl/encode and decode


1. UTF-8 to CP949 #

use Encode;
my $str = $utf8_str;
Encode::from_to($str, 'utf8', 'cp949');

use Text::Iconv;
use Text::Iconv;
$c = Text::Iconv->new("utf8","cp949");
$_data = $c->convert( $str_utf8);

2. UTF-8 Encode & Decode #

sub urlencode {
    my $s = shift;
    $s =~ s/ /+/g;
    $s =~ s/([^A-Za-z0-9\+-])/sprintf("%%%02X", ord($1))/seg;
    return $s;
}

sub urldecode {
    my $s = shift;
    $s =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;
    $s =~ s/\+/ /g;
    return $s;
}

# advanced
sub urldecode {
    #phase 1
    my $s = shift;
    $s =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;
    $s =~ s/\+/ /g;

    #phase 2
    use URI::Escape qw(uri_unescape);
    use utf8;
    my $x = my $y = uri_unescape($s);
    return $x if utf8::decode($x);
    return $y;
}

이 글에는 0 개의 댓글이 있습니다.