简体   繁体   English

在 Perl 中编码特殊字符

[英]Encoding Special Characters in Perl

I have this string for example:例如,我有这个字符串:

This is an example text ã&"><£

When I run this Perl code on the string:当我在字符串上运行这个 Perl 代码时:

my($string)= @_;
$string =~ s/>//g;
$string =~ s/<//g;
$string =~ s/&/and/g;
$string  =~ s/\"//g;
$string  =~ s/-//;
$string  =~ s/ó//;
$string =~ s/;//g;
$string =~ s/&/&amp;/g;

$string = encode_entities($string, '<>&"');
$string = encode_utf8($string);

return $string;      

I receive this result:我收到这个结果:

This is an example text ã£ã£

Instead of the expected one:而不是预期的:

This is an example text ã&amp;&quot;&gt;&lt;£

How can I solve it?我该如何解决?

Can you try the following script:你可以试试下面的脚本:

use feature qw(say);
use strict;
use warnings;
use utf8;
use open qw(:std :encoding(utf-8));
use HTML::Entities;

my $string = 'This is an example text ã&"><£';
$string = encode_entities($string, '<>&"');
say $string;

Output :输出

This is an example text ã&amp;&quot;&gt;&lt;£

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM