PHP:将 UTF-8 字符串转换为 Ansi? [英] PHP: Converting UTF-8 string to Ansi?

查看:65
本文介绍了PHP:将 UTF-8 字符串转换为 Ansi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据数据库中的值构建了一个 csv 字符串.最终的字符串存储在我的 $csv 变量中.

I build a csv string from values I have in my DB. The final string is stored in my $csv variable.

现在我提供此字符串供下载,如下所示:

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;

例如,当我在 Notepad++ 中打开它时,它显示 Ansi as UTF-8.我如何才能将其更改为仅 Ansi?

When I open this in Notepad++ for example, it says Ansi as UTF-8. How can I chnage that to Ansi only?

我试过了:

$csv = iconv("ISO-8859-1", "WINDOWS-1252", $csv);

这并没有改变任何东西.

That did not change anything.

谢谢!

解决方案:$csv = iconv("UTF-8", "WINDOWS-1252", $csv);

Solution: $csv = iconv("UTF-8", "WINDOWS-1252", $csv);

推荐答案

尝试:

$csv = iconv("UTF-8", "Windows-1252", $csv);

但你最终会丢失数据,因为 ANSI 只能编码一小部分 UTF-8.如果您没有非常强烈的理由反对它,请提供 UTF-8 编码的文件.

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.

这篇关于PHP:将 UTF-8 字符串转换为 Ansi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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