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

你离开我真会死。 提交于 2019-12-29 01:26:08

问题


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.

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?

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.

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

Thanks!


回答1:


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.




回答2:


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

<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.




回答3:


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.

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.




回答4:


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.

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



来源:https://stackoverflow.com/questions/14927122/how-to-save-russian-characters-in-a-utf-8-encoded-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!