我有一些带重音的字符串,例如“é”,目标是将我的字符串放入 URL,因此我需要将“é”转换为“%C3%A9”
我已经测试了一些模块作为 HTML::Entitie
、Encode
或 URI::Encode
但没有成功
实际结果:
%C3%83%C2%A9
预期结果:
%C3%A9
#!/usr/bin/perl
use strict;
use warnings;
use HTML::Entities;
use feature 'say';
use URI::Encode qw( uri_encode );
my $var = "é";
say $var;
$var = uri_encode( $var );
say $var;
最佳答案
您缺少使用utf8
。
The use utf8 pragma tells the Perl parser to allow UTF-8 in the program text in the current lexical scope. The no utf8 pragma tells Perl to switch back to treating the source text as literal bytes in the current lexical scope. (On EBCDIC platforms, technically it is allowing UTF-EBCDIC, and not UTF-8, but this distinction is academic, so in this document the term UTF-8 is used to mean both).
Do not use this pragma for anything else than telling Perl that your script is written in UTF-8. The utility functions described below are directly usable without use utf8;.
关于perl - 是否可以在 Perl 中将 'é' 打印为 '%C3%A9' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71165599/