php 如何将俄语字符保存在 UTF-8 编码文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14927122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:13:25  来源:igfitidea点击:

How to save Russian characters in a UTF-8 encoded file

phputf-8translate

提问by Sam G

OK so I have a PHP file with several strings of text in various languages. For most languages like French or Spanish I just simply type in the characters.

好的,所以我有一个 PHP 文件,其中包含多个不同语言的文本字符串。对于法语或西班牙语等大多数语言,我只需输入字符即可。

The problem I have is with Russian language characters. The PHP file is encoded in UTF-8, how can I make sure that the Russian characters are both saved correctly and displayed correctly on the output web page... Is it just a case of pasting the text into the PHP file, or is there a way to guarantee the characters will be saved into the file correctly - perhaps converting it into HTML-like notation for example?

我的问题是俄语字符。PHP 文件以 UTF-8 编码,如何确保俄语字符都正确保存并正确显示在输出网页上...是否只是将文本粘贴到 PHP 文件中的情况,还是有没有办法保证字符将正确保存到文件中 - 例如,可能将其转换为类似 HTML 的符号?

Obviously I am assuming the end user will have the correct encoding set in their web browser, I just want to make sure I got it all covered from my end.

显然,我假设最终用户将在他们的 Web 浏览器中设置正确的编码,我只是想确保我从我的最后得到了所有这些。

I am using Notepad++ on Windows to edit my PHP file.

我在 Windows 上使用 Notepad++ 来编辑我的 PHP 文件。

Thanks!

谢谢!

回答by vikingmaster

If you want to tell browsers your encoding, place it inside your <header>tag:

如果你想告诉浏览器你的编码,把它放在你的<header>标签中:

<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>

Or short version

或短版

<meta charset='utf-8'>

That should be pretty enough for Russian characters to be correctly displayed on a webpage.

这应该足以让俄语字符正确显示在网页上。

回答by Andre Chenier

if your doctype is html declare <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>but if your doctype is xhtml then declare <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />.

Never assume that end-user will act correctly during your designs

If you already have some document, edit your document's meta tag for charset declaration and use notepad++ encoding>convert to UTF-8 without BOM, save your document, safely go on with your multilingual structure from now on.

php tag is irrelevant for your question since you don't mention about any database char setting.

如果您的 doctype 是 html 声明<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>但如果您的 doctype 是 xhtml 则声明<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />.

永远不要假设最终用户会在您的设计过程中正确操作

如果您已经有一些文档,请编辑文档的字符集声明的元标记并使用 notepad++ 编码>转换为没有 BOM 的 UTF-8,保存您的文档,安全地继续您的多语言结构从现在开始。

php 标记与您的问题无关,因为您没有提到任何数据库字符设置。

回答by fuxia

There is no difference between Latin and Cyrillic characters in UTF-8. Both are just byte sequences. Configure your server or PHP script to send Content-Type: text/html;charset=utf, and you are rather safe.

UTF-8 中的拉丁字符和西里尔字符没有区别。两者都只是字节序列。将您的服务器或 PHP 脚本配置为 send Content-Type: text/html;charset=utf,您就相当安全了。

Your editor might have problems when the font you are using does not contain Russian characters. Choose another font then.

当您使用的字体不包含俄语字符时,您的编辑器可能会出现问题。然后选择另一种字体。

And please ignore the <meta>element recommendations. You don't need that: it is useless when your HTTP headers are correct, and maybe harmful if they aren't.

请忽略<meta>元素建议。您不需要那个:当您的 HTTP 标头正确时它是无用的,如果它们不正确则可能有害。

回答by Stepo

Well you have to check 2 things

那么你必须检查两件事

  • To ensure that *.php is an UTF-8 file I use PSPad. If file is not in UTF-8, I save it like that: http://stepolabs.com/upload/utf-8.png
  • Then your website must have UTF-8 encoding in <meta>tag;

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    ... more about metatagging.

  • 为了确保 *.php 是一个 UTF-8 文件,我使用PSPad。如果文件不是 UTF-8,我会这样保存:http: //stepolas.com/upload/utf-8.png
  • 那么你的网站<meta>标签中必须有UTF-8编码;

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    ...更多关于元标记

Finally if everything is done well - (format and meta declaration) all should be displayed properly!

最后,如果一切顺利 - (格式和元声明)都应该正确显示!