为什么我的输出会改变? [英] Why does my output change?

查看:98
本文介绍了为什么我的输出会改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP中的UTF-8编码,我一直设法获得所需的输出.然后,在代码未发生任何变化的情况下,输出突然发生了变化.

I'm working with UTF-8 encoding in PHP and I keep managing to get the output just as I want it. And then without anything happening with the code, the output all of a sudden changes.

以前,我得到希伯来语输出.现在我收到&&&&".

Previously I was getting hebrew output. Now I'm getting "&&&&&".

任何想法可能是什么原因造成的?

Any ideas what might be causing this?

推荐答案

这些是最常见的问题:

  • 您要在其中创建PHP/HTML文件的编辑器
  • 您正在通过其浏览站点的Web浏览器
  • 您在Web服务器上运行的PHP Web应用程序
  • MySQL数据库
  • 您要从其他任何地方读取/写入数据(内存缓存,API,RSS提要等)

您可以尝试的几件事:

配置编辑器

请确保您的文本编辑器,IDE或您编写PHP代码的任何内容均以UTF-8格式保存文件.您的FTP客户端,SCP,SFTP客户端不需要任何特殊的UTF-8设置.

Ensure that your text editor, IDE or whatever you’re writing the PHP code in saves your files in UTF-8 format. Your FTP client, scp, SFTP client doesn’t need any special UTF-8 setting.

确保网络浏览器知道使用UTF-8

要确保您的用户的浏览器都知道将所有数据读/写为UTF-8,可以在两个位置进行设置.

To make sure your users’ browsers all know to read/write all data as UTF-8 you can set this in two places.

内容类型标记

确保内容类型META标头将UTF-8指定为这样的字符集:

Ensure the content-type META header specifies UTF-8 as the character set like this:

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">

HTTP响应标头

确保Content-Type响应标头也将UTF-8指定为字符集,如下所示:

Make sure that the Content-Type response header also specifies UTF-8 as the character-set like this:

ini_set('default_charset', 'utf-8')

配置MySQL连接

现在,您知道您从用户那里收到的所有数据都是UTF-8格式,我们需要配置PHP和MySQL数据库之间的客户端连接. 只需执行MySQL查询,就有一种通用的方法:

Now you know that all of the data you’re receiving from the users is in UTF-8 format we need to configure the client connection between the PHP and the MySQL database. There’s a generic way of doing by simply executing the MySQL query:

SET NAMES utf8;

...并且根据您使用的客户端/驱动程序,可以使用助手功能来更轻松地执行此操作:

…and depending on which client/driver you’re using there are helper functions to do this more easily instead:

具有内置的mysql函数

mysql_set_charset('utf8', $link);

使用MySQLi

$mysqli->set_charset("utf8")

*使用PDO_MySQL(在您连接时)*

*With PDO_MySQL (as you connect)*

$pdo = new PDO( 
    'mysql:host=hostname;dbname=defaultDbName', 
    'username', 
    'password', 
    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") 
);

MySQL数据库

我们现在已经很多了,您只需要确保MySQL知道将数据以UTF-8格式存储在表中即可.您可以通过查看SHOW TABLE STATUS的输出中的Collat​​ion值来检查其编码(在phpmyadmin中,这在表列表中显示). 如果您的表尚未位于UTF-8中(很可能它们位于latin1中),则需要通过为每个表运行以下命令来对其进行转换:

We’re pretty much there now, you just need to make sure that MySQL knows to store the data in your tables as UTF-8. You can check their encoding by looking at the Collation value in the output of SHOW TABLE STATUS (in phpmyadmin this is shown in the list of tables). If your tables are not already in UTF-8 (it’s likely they’re in latin1) then you’ll need to convert them by running the following command for each table:

ALTER TABLE myTable CHARACTER SET utf8 COLLATE utf8_general_ci;

需要提防的最后一件事

现在完成所有这些步骤,您的应用程序应该没有任何字符集问题. 需要注意的是,大多数PHP字符串函数都不支持Unicode,因此,例如,如果对多字节字符运行strlen(),它将返回输入中的字节数,而不是输入的字节数.人物.您可以使用Multibyte String PHP扩展来解决此问题,尽管这些字节/字符问题引起问题的情况并不常见.

With all of these steps complete now your application should be free of any character set problems. There is one thing to watch out for, most of the PHP string functions are not unicode aware so for example if you run strlen() against a multi-byte character it’ll return the number of bytes in the input, not the number of characters. You can work round this by using the Multibyte String PHP extension though it’s not that common for these byte/character issues to cause problems.

在此处填写表格: http://webmonkeyuk.wordpress.com/2011/04/23/how-to-avoid-character-encoding-problems-in-php/

这篇关于为什么我的输出会改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆