【问题标题】:ISO decoding issue perlISO解码问题perl
【发布时间】:2013-10-24 12:45:34
【问题描述】:

我有包含的字符串

$string = "&®";

当我执行HTML::Entities::decode($string); 时,它返回给我&®,我发现此函数无法解码ISO 代码http://www.w3schools.com/tags/ref_entities.asp

然后我尝试了其他一些方法,但没有成功

Encode::decode('utf8', '®') // returns ®
Encode::decode_utf8('®') // returns ®

然后我尝试使用正则表达式模式手动替换它,

$string =~ s/®/®/g;

但是上面的行把它转换成&®,谁能告诉我如何解码这些ISO字符,如果手动替换为什么会出现 ?

【问题讨论】:

  • 实体的权威文档是in the HTML spec,不在不可靠的w3schools中。无论如何,我无法重现:perl -CS -MHTML::Entities -E'say decode_entities "&®"' 在 perl v5.18.1 上使用最新的 H:E v3.69 产生预期的输出。你用的是什么版本?
  • perl 5.14.2 也正常输出(HTML::Entities 3.69 也是)
  • 回复:“它返回给我&®”,这不是真的,它返回&®
  • 回复:“但是上面的行把它转换成&®”,你没有提供足够的信息。请回答以下三个问题:您的脚本编码是什么?你用use utf8;了吗?你的终端期望什么编码?
  • @ikegami 是的,我正在尝试将一些 html 编码文本解码为 UTF8,以便我可以看到 ®到®

标签: regex perl encoding


【解决方案1】:

尝试use utf8;。这对我有用:

use strict;
use warnings;
use utf8;
use Encode;

my $s = '®';

$s =~ s/®/®/g;

print encode('utf8', $s);

【讨论】:

  • 在 utf-8 编码后变成了 ®
  • 请回答 ikegami 和 amon 的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-18
相关资源
最近更新 更多