目前我的网站支持英语、葡萄牙语、瑞典语和波兰语。但由于某种原因,一些波兰语字符显示不正确,就像Zal?z konto它应该是这样的Zalóz konto
我有这个
// Send the Content-type header in case the web server is setup to send something else
header('Content-type: text/html; charset=utf-8');
Run Code Online (Sandbox Code Playgroud)
和里面 <head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
Run Code Online (Sandbox Code Playgroud)
您还需要将字符串转换为 UTF8。
utf8_encode() 不检查你的字符串是什么编码,有时它会给你一个混乱的字符串,所以我做了一个名为 Encoding::toUTF8() 的函数来正确地做到这一点。
您不需要知道字符串的编码是什么。它可以是 Latin1 (iso 8859-1)、Windows-1252 或 UTF8,或者字符串可以是它们的混合。Encoding::toUTF8() 会将所有内容转换为 UTF8。
我这样做是因为一项服务给我提供了一个全乱七八糟的数据源,将这些编码混合在同一个字符串中。
用法:
$utf8_string = Encoding::toUTF8($mixed_string);
$latin1_string = Encoding::toLatin1($mixed_string);
Run Code Online (Sandbox Code Playgroud)
我已经包含了另一个函数,Encoding::fixUTF8(),它将修复每个 UTF8 字符串,这些字符串看起来是多次编码为 UTF8 的乱码产品。
用法:
$utf8_string = Encoding::fixUTF8($garbled_utf8_string);
Run Code Online (Sandbox Code Playgroud)
例子:
echo Encoding::fixUTF8("Fédération Camerounaise de Football");
echo Encoding::fixUTF8("Fédération Camerounaise de Football");
echo Encoding::fixUTF8("FÃÂédÃÂération Camerounaise de Football");
echo Encoding::fixUTF8("Fédération Camerounaise de Football");
Run Code Online (Sandbox Code Playgroud)
将输出:
Fédération Camerounaise de Football
Fédération Camerounaise de Football
Fédération Camerounaise de Football
Fédération Camerounaise de Football
Run Code Online (Sandbox Code Playgroud)
下载:
http://dl.dropbox.com/u/186012/PHP/forceUTF8.zip
如果你使用 php 从 mysql 数据库检索数据,你应该在做任何事情之前使用这个查询。
mysql_query("SET NAMES utf8");
因此,如果从数据库接收到的数据正确存储在其中,则它们将被正确编码......