开发者

Write a file with encoding UTF-8 in php

开发者 https://www.devze.com 2023-03-19 03:40 出处:网络
P.S.: It is not a duplicated question, because I\'m not looking to write contents in a file because it is already done, I\'m looking to change a type of a file to be UTF-8, there is a difference in it

P.S.: It is not a duplicated question, because I'm not looking to write contents in a file because it is already done, I'm looking to change a type of a file to be UTF-8, there is a difference in it.

How to generate the UTF-8 file and not ANSI. (Is not the contents).

For example, the most IDE have an option encoding, where you are able to modify the type of your file, but I'm generating a bulk from my database, and it generates a lot of individual text files, but the whole files is ANSI default.. I'm just looking for a function in php that make it possible to change the encoding before it generates the bulk.

If the source code help I can post it here. just let me know.

Thanks in advance.

EDITED

Follow a print of what I'm asking here.

Write a file with encoding UTF-8 in php

When I generate the file "testecli01.csv" it always ge开发者_JS百科t encoding ANSI, whatever I do in my script it is always ANSI, and I need in UTF-8, just this. Is simple but I have no idea how to do.


If your 3rd party program "do not support files in ANSI but UTF-8" as you mentioned in a comment then most likely it's expecting a BOM.

While the Unicode Standard does allow a BOM in UTF-8,[2] it does not require or recommend it.[3] Byte order has no meaning in UTF-8[4] so a BOM serves only to identify a text stream or file as UTF-8.

The reason the BOM is recommended against is that it defeats the ASCII back-compatibility that is part of UTF-8's design.

So strictly speaking your 3rd party program isn't completely compliant with the standard because the BOM should be optional. ANSI is 100% valid UTF-8 and that is one of the main drivers of it. Anything that can understand UTF-8 accordng to the standard by definition also understands ANSI.

Try writing "\xEF\xBB\xBF" to the front of the file and see if that solves your problem.


I do not know of a database that will do the encoding conversion for you easily. For example, in MySQL, you have to reset all the character encodings for the db, tables, and columns, AND THEN convert the data.

I would suggest instead that you create your database dump and use iconv to change the encoding, whether on the command line:

iconv -f original_charset -t utf-8 dumpTextData > convertedTextData

or in PHP (taken from How to write file in UTF-8 format?)

$input = fopen($file, 'r');
$output = fopen($file, 'w');
stream_filter_append($input, 'convert.iconv.UTF-8/OLD-ENCODING');
stream_copy_to_stream($input, $output);
fclose($input);
fclose($output);

NOTE: edited to avoid leaking file descriptors.


Excel likes CSV files to be UTF-16LE, and begin with '\xFF\xFE'.

My code to build a file for excel is:

echo "\xFF\xFE"; // marker for UTF-16 file;

foreach ($rows as $row)
    echo mb_convert_encoding($row, 'UTF-16LE');


Old encoding is first, as it is in iconv function. You also can´t read and write same file.

    $input = fopen($path, 'r');
    $output = fopen($path . '.tmp', 'w');
    stream_filter_append($input, 'convert.iconv.OLDENCODING/UTF-8');
    stream_copy_to_stream($input, $output);
    fclose($input);
    fclose($output);
    unlink($path);
    rename($path . '.tmp', $path);
0

精彩评论

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

关注公众号