开发者

Specify .doc file encoding when creating with php?

开发者 https://www.devze.com 2023-02-03 20:48 出处:网络
Currently i try to create some of my database export work more automated and i decided to go with exporting data from MySql database to .doc file in php.

Currently i try to create some of my database export work more automated and i decided to go with exporting data from MySql database to .doc file in php.

So im doing some database work and store complete html to a $str variable and then write/make new file:

$fp = fopen($file, 'w+'); fwrite($fp,$str); fclose($fp);

The problem here is not creating .doc file, but when i 开发者_C百科download and view that .doc file it's not in UTF-8 it's in unicode.


You do realize that giving a file a .doc extension doesn't make it a Word document?

Anyway, if your $str and HTML was encoded in UTF-8, so should be the downloaded file. Otherwise send it with an appropriate

header("Content-Type: text/html; charset=UTF-8");

It cannot reasonably be "in unicode". You need to explain what that means. It's certainly not autoconverted from UTF-8 to UCS-4 (any of the 4-byte raw unicode representations).

However you could include the UTF-8 BOM to make it certain:

fwrite($fp, pack("HHH", 0xEF, 0xBB, 0xBF));
0

精彩评论

暂无评论...
验证码 换一张
取 消