简体   繁体   English

Perl和HTML / JavaScript中的特殊字符(非英语)

[英]Special characters (non-English) in Perl and HTML/JavaScript

I have text I am trying to display that is being sent from Perl to my web page. 我正在尝试显示从Perl发送到我的网页的文本。 In Perl I have this set 在Perl中,我有这套

print "Content-Type: text/plain; charset=UTF-8\n\n";

And I am pretty sure I need to change the charset setting. 而且我很确定我需要更改字符集设置。 My first question is how can I change the setting to include more languages not to just change the language. 我的第一个问题是如何更改设置以包括更多语言,而不仅仅是更改语言。

My second question is if I set the charset straight in Perl do I also need to change it on my HTML/JavaScript so that the web page the information is being displayed on displays it correctly? 我的第二个问题是,如果我在Perl中直接设置字符集,是否还需要在HTML / JavaScript上对其进行更改,以使正在显示信息的网页正确显示它?

Do not forget to decode and encode text based on source encoding! 不要忘记基于源编码对文本进行解码和编码!

#!/usr/bin/perl

use strict;
use warnings;
use CGI ":all";
use Encode;

my $cgi = new CGI;
binmode STDOUT, ':utf8';

print $cgi->header(-type    => 'text/html',
                   -charset => 'utf-8');

print $cgi->start_html(-title => 'Test',
                       -charset  => 'utf-8',
                       -encoding => 'utf-8',
                       -head => meta({-http_equiv => 'Content-Type',
                                      -content => 'text/html; charset=utf-8'}));

my $text = ...

Encode::from_to($text, 'latin1', 'utf8');

print $cgi->p($text);
print $cgi->end_html;

My first question is how can I change the setting to include more languages 我的第一个问题是如何更改设置以包含更多语言

UTF-8 includes (almost) all languages. UTF-8包含(几乎)所有语言。 Perhaps you mean "different encodings"? 也许您的意思是“不同的编码”? In that case, you need to know what encoding your source is in. Where did it come from before it passed through perl? 在这种情况下,您需要知道源编码是什么。在通过perl 之前 ,源来自哪里?

UTF-8 is also the predominant encoding online, and you're probably best to stick with it. UTF-8也是在线编码的主要方式,您最好坚持使用它。 That means if you have a source that is not in utf8, you should use perl's decode() and encode() first. 这意味着,如果您的源代码不在utf8中,则应首先使用perl的encode()和encode()。 But again: you do need to know what the source encoding is. 再说一遍:您确实需要知道源编码是什么。

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

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