PHP:将 UTF-8 字符串转换为 Ansi?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14933929/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: Converting UTF-8 string to Ansi?
提问by user1856596
I build a csv string from values I have in my DB. The final string is stored in my $csv variable.
我从数据库中的值构建了一个 csv 字符串。最后的字符串存储在我的 $csv 变量中。
Now I offer this string for download, like this:
现在我提供这个字符串供下载,如下所示:
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=whatever.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo $csv;
When I open this in Notepad++ for example, it says Ansi as UTF-8. How can I chnage that to Ansi only?
例如,当我在 Notepad++ 中打开它时,它说Ansi 为 UTF-8。我怎么能把它改成 Ansi 呢?
I tried:
我试过:
$csv = iconv("ISO-8859-1", "WINDOWS-1252", $csv);
That did not change anything.
这并没有改变任何事情。
Thanks!
谢谢!
Solution:$csv = iconv("UTF-8", "WINDOWS-1252", $csv);
解决方案:$csv = iconv("UTF-8", "WINDOWS-1252", $csv);
回答by Fabian Schmengler
Try:
尝试:
$csv = iconv("UTF-8", "Windows-1252", $csv);
But you will eventually lose data because ANSI can only encode a small subset of UTF-8. If you don't have a very strong reason against it, serve your files UTF-8 encoded.
但是您最终会丢失数据,因为 ANSI 只能对一小部分 UTF-8 进行编码。如果您没有非常强烈的理由反对它,请为您的文件提供 UTF-8 编码。
回答by Borislav Sabev
Since there is a misunderstanding about ISO-8859-1, Windows-1252 & ANSI in your question an important thing to note here is that:
由于您的问题中对 ISO-8859-1、Windows-1252 和 ANSI 存在误解,因此这里需要注意的重要一点是:
The so-called Windows character set (WinLatin1, or Windows code page 1252, to be exact) uses some of those positions for printable characters. Thus, the Windows character set is NOT identical with ISO 8859-1. The Windows character set is often called "ANSI character set", but this is SERIOUSLY MISLEADING. It has NOT been approved by ANSI.
Historical background: Microsoft based the design of the set on a draft for an ANSI standard. A glossary by Microsoft explicitly admits this.
所谓的 Windows 字符集(WinLatin1,或 Windows 代码页 1252,确切地说)使用其中一些位置作为可打印字符。因此,Windows 字符集与 ISO 8859-1 不同。Windows 字符集通常称为“ANSI 字符集”,但这是严重误导。它尚未得到 ANSI 的批准。
历史背景:Microsoft 基于 ANSI 标准的草案设计了该系列。微软的词汇表明确承认这一点。
Some more resources: hereand here.
So just FYI for other people that end up in this question.
所以仅供参考,其他人最终会遇到这个问题。
Here's MS's exact explanation on this:
这是 MS 对此的确切解释:
The term “ANSI” as used to signify Windows code pages is a historical reference, but is nowadays a misnomer that continues to persist in the Windows community. The source of this comes from the fact that the Windows code page 1252 was originally based on an ANSI draft—which became International Organization for Standardization (ISO) Standard 8859-1. “ANSI applications” are usually a reference to non-Unicode or code page–based applications.
用于表示 Windows 代码页的术语“ANSI”是一个历史参考,但如今在 Windows 社区中仍然存在。其根源在于 Windows 代码页 1252 最初基于 ANSI 草案——后来成为国际标准化组织 (ISO) 标准 8859-1。“ANSI 应用程序”通常是对非 Unicode 或基于代码页的应用程序的引用。
回答by Zebx
To avoid data loss when converting special characters:
为避免在转换特殊字符时丢失数据:
setlocale(LC_CTYPE, "fr_FR.UTF-8"); //set your own locale
$csv = iconv("UTF-8", "WINDOWS-1252//TRANSLIT//IGNORE", $csv);

