简体   繁体   English

PHP:西里尔字母不能正确显示

[英]PHP: Cyrillic characters not displayed correctly

Recently I switched hosting from one provider to the other and I have problems displaying Cyrillic characters. 最近,我将托管服务从一个提供商切换到了另一个提供商,但在显示西里尔字母时遇到了问题。 The characters which are read from the database are displayed correctly, but characters which are hardcoded in the php file aren't (they are displayed as question marks). 从数据库读取的字符正确显示,但是在php文件中硬编码的字符却没有显示(它们显示为问号)。

The files which contain the php source code are saved in utf-8 form. 包含php源代码的文件以utf-8格式保存。 Help anybody? 帮助任何人?

尝试在标头部分放置一个表示编码的meta标签:

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

My PHP module was exactly with same problem all text was like "?????????????????" 我的PHP模块完全有同样的问题,所有文本都像“ ?????????????????”

And my code was written with Notepad++. 我的代码是用Notepad ++编写的。 I found the solution for the problem. 我找到了解决问题的方法。 The problem wasn't in the header charset or meta tag because the browser actually knows that it is the UTF-8 charset. 问题不在标题字符集或元标记中,因为浏览器实际上知道它是UTF-8字符集。 I tried all encodings from the browser and the result was the same so I knew the problem is somewhere else, not in the browser character encoding at all. 我从浏览器尝试了所有编码,结果都是一样的,所以我知道问题出在别的地方,根本不是浏览器的字符编码。

I just opened the PHP module with Notepad++ and selected all code. 我刚刚用Notepad ++打开了PHP模块并选择了所有代码。 After that in the Encoding menu I selected "Convert to UTF-8." 之后,在“编码”菜单中,我选择了“转换为UTF-8”。 After uploading to the server, everything worked like a charm. 上传到服务器后,所有内容都像一个超级按钮。

The problem seems quite strange. 这个问题似乎很奇怪。
What's the form of these question marks? 这些问号的形式是什么? Is it black diamonds with questions or just plain question marks? 是带有疑问的黑钻石还是仅仅是问号?
First of all double check if your files are really utf-8 encoded. 首先,请仔细检查您的文件是否真的是utf-8编码。
Try to add this header to your code (above all output) 尝试将此标头添加到您的代码中(在所有输出上方)

header('Content-Type: text/html; charset=utf-8'); 

But I doubt it would help, as your database text already looks good. 但是我怀疑这会有所帮助,因为您的数据库文本已经看起来不错。
Do you have any SET NAMES queries in your code? 您的代码中是否有任何SET NAMES查询? What charset it is set? 它设置了什么charset集?

put this after connecting database: 在连接数据库后放这个:

mysql_query("SET NAMES UTF8");

for mysqli 对于mysqli

mysqli_query($connecDB,"SET NAMES UTF8");

and in the header of the page: 并在页面标题中:

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

It had something to do with the encoding of the php files. 它与php文件的编码有关。 The files were created using Windows Notepad and saved with utf-8 encoding. 这些文件是使用Windows记事本创建的,并以utf-8编码保存。

When I used Notepad2 to open the files, the encoding of the files was "utf-8 with signature". 当我使用Notepad2打开文件时,文件的编码为“带签名的utf-8”。 When I changed encoding to "utf-8", the text displayed correctly. 当我将编码更改为“ utf-8”时,文本正确显示。

The reason for your problem is often accidental re-encoding the script files by a programmer's editor. 出现问题的原因通常是程序员的编辑器意外地重新编码了脚本文件。 It isn't a good practice to hardcode strings which rely on encoding in your php files. 硬编码依赖于php文件中编码的字符串不是一个好习惯。

Try switching your browser's encoding to find what encoding is used for hardcoded text, it might help you address the issue. 尝试切换浏览器的编码,以找到用于硬编码文本的编码,这可能有助于您解决此问题。 Also make sure to send proper http headers for each page: 还要确保为每个页面发送正确的http标头:

header('Content-Type: text/html; charset=utf-8');

Optionaly you can insert meta tag in you HTML: 可选地,您可以在HTML中插入meta标签:

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

For me, the line that made the difference is the following: 对我而言,造成变化的那一行如下:

$mysqli->set_charset("utf8")

Straight from the PHP documentation page . 直接来自PHP文档页面

The server was returning latin, after setting the charset to utf8 now works fine. 将字符集设置为utf8后,服务器返回拉丁语,现在可以正常工作。

I have been fighting with this exact same problem as I'm trying to add a bit of french/german internationalization to a few controls on a widget. 我一直在与这个完全相同的问题作斗争,因为我试图在小部件上的一些控件中添加一些法语/德语国际化。

Characters with accents that are stored in my db print fine as UTF-8. 存储在我的数据库中的带有重音符号的字符可以打印为UTF-8。 However, characters that are hardcoded into arrays in PHP files either display as the black diamond with a question mark inside or the little square box. 但是,在PHP文件中硬编码为数组的字符要么显示为内部带有问号的黑色菱形,要么显示为小方框。

I've tried encoding/decoding the hardcoded strings from my php file every which way, but couldn't get the characters to display properly. 我已经尝试过以各种方式对我的php文件中的硬编码字符串进行编码/解码,但是无法正确显示字符。

Since I have such a finite set of characters and am working strictly with HTML, I just added a bit of functionality to my intl class to substitute the characters for html entities. 由于我的字符集非常有限,并且严格使用HTML,因此我向intl类添加了一些功能,以将字符替换为html实体。

I have these properties. 我有这些属性。

static $accentEntities = array('á' => '&aacute;', 
                               'É' => '&Eacute;',
                               'é' => '&eacute;', 
                               'í' => '&iacute;', 
                               'û' => '&ucirc;', 
                               'ü' => '&uuml;');
static $accents = array();
static $entities = array();

I setup some my replacement arrays in my constructor... 我在构造函数中设置了一些替换数组...

foreach (self::$accentEntities as $char => $entity) {
  self::$accents[] = $char;
  self::$entities[] = $entity;
}

And then when I need one of my hardcoded strings in my class I just return it like so... 然后,当我在班级中需要一个硬编码的字符串之一时,我就这样返回它...

return str_replace(self::$accents,self::$entities,$str);

It's a totally ghetto solution... but for now, it works. 这是一个完全的贫民窟解决方案……但目前为止,它仍然有效。 I'd definitely like to hear the correct way to display accents/special characters that are hardcoded into a PHP file. 我绝对希望听到正确的方法来显示被硬编码到PHP文件中的重音符号/特殊字符。

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

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