强制对字符串执行ANSI编码(CP1252,ISO8859),强制下载时获取UTF-8编码 [英] Forcing an ANSI encoding on string (CP1252, ISO8859), obtaining UTF-8 encoding when force download it

查看:165
本文介绍了强制对字符串执行ANSI编码(CP1252,ISO8859),强制下载时获取UTF-8编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将其强制转换为起始字符串:

If I cast this on my starting string:

echo mb_detect_encoding($string);

我得到的值:

ASCII

ASCII

此字符串将被下载.我想它是UTF-8,因为这是PHP以及数据库的默认编码.它的文件扩展名为.DAT,我已经通过以下方式将其添加到config/mimes.php:

This string will be downloaded. I suppose it's UTF-8, as that's the default encoding for PHP as well as from the database. Its file extension will be .DAT, and I have already added it to config/mimes.php this way:

'DAT' => 'text/plain; charset=ISO-8859-1'

然后,如果我尝试使用Codeigniter的下载助手来下载该字符串(假设我已经加载了该助手)

Then if I try to download that string, using the download helper of Codeigniter (assume I have already loaded the helper)

force_download('MYFILE.DAT', $string);

通过F12进行调试,响应头如下:

Debugging via F12, response headers are the following:

Content-Disposition:attachment; filename="MYFILE.DAT"
Content-Length:21024
Content-Transfer-Encoding:binary
Content-Type:"text/plain; charset=ISO-8859-1"

但是当我在Notepad ++中打开此文件时,它似乎是用UTF-8编码的,而没有BOM.

But when I open this file in Notepad++, it appears to be encoded in UTF-8 without BOM.

我什至尝试应用iconv以及mb_convert_encoding,就好像该字符串在utf-8中一样(即使mb_detect_encoding告诉我该字符串是ASCII):

I have even tried to apply iconv, as well as mb_convert_encoding as if the string was in utf-8, (even though mb_detect_encoding told me the string was ASCII):

iconv("UTF-8", "ISO-8859-1", $string);
iconv("UTF-8", "CP1252", $string);
/* ... and so on ... */

也尝试过

mb_convert_encoding($string, "ISO-8859-1");
mb_convert_encoding($string, "CP1252");
/* ... and so on ... */

但显然,结果相同.如果我执行var_dump(重音弄乱了),字符串似乎是ISO-8859(但重音被弄乱了),但是下载后,它似乎仍以UTF-8编码(重音又回来了!)

But obviously, had same results. String appears to be ISO-8859 if I do a var_dump (accents are messed up) but after downloading, seems to be still encoded in UTF-8 (accents are back again!)

我在这里想念什么?我究竟做错了什么?我应该先写入文件然后再强制下载吗?

What am I missing here? What am I doing wrong? Should I write the file before and then force download it?

已解决:

问题是起始字符集.它似乎在ISO-8859-1中.尽管@deceze不能在纯文本文件中指定编码这一事实绝对正确,但实际上您可以对它的内容(字符)进行编码.

The problem was the starting charset. It appeared to be in ISO-8859-1. Despite @deceze was absolutely right about the fact you can't specify the encoding in a plain text file, you actually are able to encode its contents, I mean, the characters.

推荐答案

您无法检测具有任何一致性或准确性的编码. ASCII文件在ISO-8859或UTF-8或任何其他与ASCII兼容的编码中均有效. PHP默认将其称为ASCII,Notepad ++默认将其称为UTF-8.两项决定均有效.由于实际"编码未存储在文件中或文件元数据中(即使您设置了HTTP标头),因此没有正确"答案.

You cannot detect encodings with any measure of consistency or accuracy. An ASCII file is just as valid in ISO-8859 or UTF-8 or any other ASCII-compatible encoding. PHP defaults to calling it ASCII, Notepad++ defaults to calling it UTF-8. Both decisions are equally valid. Since the "actual" encoding is not stored anywhere in the file or with the file's metadata (even if you set HTTP headers), there is no "right" answer.

这篇关于强制对字符串执行ANSI编码(CP1252,ISO8859),强制下载时获取UTF-8编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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