Илья Савич
Илья Савич

Reputation: 734

Adding BOM to PHP output stream

I want to export some data with pound sign to CSV file and I adding BOM for correct representation of pound. I faced with situation when I'm writing to php output stream php://output BOM was somewhere ignored so that in file there was no this 3 bytes. Interesting fact if you duplicate BOM string (something like \xef\xbb\xbf\xef\xbb\xbf) the first 3 bytes will be ignored and in file there will be only last 3 bytes. This is reproducing only for writing in output stream. When I tried to write in real file all works as expected.

EDIT: To clarify my question. Maybe someone know why it is works so? And how can I solve the problem without hacks such as duplicate BOM string

EDIT: I'm using Symfony 2.8 StreamedResponse. So the code works as

$f = fopen('php://output', 'r+');
fwrite($f, "\xef\xbb\xbf\xef\xbb\xbf"); // only 3 bytes will exists
// other code. fputcsv(...) .. fflush($f)

And files I'm checking with https://hexed.it/

Upvotes: 0

Views: 1862

Answers (1)

borN_free
borN_free

Reputation: 1482

Seems like this is how UTF-8 decode algorithm works in browsers, see https://stackoverflow.com/a/42717677

Upvotes: 2

Related Questions