PHP:将“”转换为“”时出现问题从 ISO-8859-1 到 UTF-8 的字符
我在使用 PHP 将 ISO-8859-1 数据库内容转换为 UTF-8 时遇到一些问题。我正在运行以下代码来测试:
// Connect to a latin1 charset database
// and retrieve "Georgia O’Keeffe", which contains a "’" character
$connection = mysql_connect('*****', '*****', '*****');
mysql_select_db('*****', $connection);
mysql_set_charset('latin1', $connection);
$result = mysql_query('SELECT notes FROM categories WHERE id = 16', $connection);
$latin1Str = mysql_result($result, 0);
$latin1Str = substr($latin1Str, strpos($latin1Str, 'Georgia'), 16);
// Try to convert it to UTF-8
$utf8Str = iconv('ISO-8859-1', 'UTF-8', $latin1Str);
// Output both
var_dump($latin1Str);
var_dump($utf8Str);
当我在 Firefox 的源代码视图中运行此代码时,确保 Firefox 的编码设置设置为“Western (ISO-8859-1)”,我得到:
到目前为止,一切顺利。第一个输出包含那个奇怪的引用,我可以正确地看到它,因为它在 ISO-8859-1 中,Firefox 也是如此。
当我将 Firefox 的编码设置更改为“UTF-8”后,它看起来像这样:
引用去了哪里? iconv()
不是应该将其转换为 UTF-8 吗?
I'm having some issues with using PHP to convert ISO-8859-1 database content to UTF-8. I am running the following code to test:
// Connect to a latin1 charset database
// and retrieve "Georgia O’Keeffe", which contains a "’" character
$connection = mysql_connect('*****', '*****', '*****');
mysql_select_db('*****', $connection);
mysql_set_charset('latin1', $connection);
$result = mysql_query('SELECT notes FROM categories WHERE id = 16', $connection);
$latin1Str = mysql_result($result, 0);
$latin1Str = substr($latin1Str, strpos($latin1Str, 'Georgia'), 16);
// Try to convert it to UTF-8
$utf8Str = iconv('ISO-8859-1', 'UTF-8', $latin1Str);
// Output both
var_dump($latin1Str);
var_dump($utf8Str);
When I run this in Firefox's source view, making sure Firefox's encoding setting is set to "Western (ISO-8859-1)", I get this:
So far, so good. The first output contains that weird quote and I can see it correctly because it's in ISO-8859-1 and so is Firefox.
After I change Firefox's encoding setting to "UTF-8", it looks like this:
Where did the quote go? Wasn't iconv()
supposed to convert it to UTF-8?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
U+2019 右单引号不是 ISO-8859-1 中的字符。它是 windows-1252 中的一个字符,如 0x92。实际的 ISO-8859-1 字符 0x92 是一个很少使用的 C1 控制字符,称为“私人使用” 2”。
看来这就是这里正在发生的事情。将“ISO-8859-1”更改为“windows-1252”。
U+2019 RIGHT SINGLE QUOTATION MARK is not a character in ISO-8859-1. It is a character in windows-1252, as 0x92. The actual ISO-8859-1 character 0x92 is a rarely-used C1 control character called "Private Use 2".
It appears that this is what's happening here. Change "ISO-8859-1" to "windows-1252".
假设您的页面标题
charset
是 utf-8,这将解决您的问题:this will solve your problem, supposing that your page header
charset
is utf-8: