Page 1 of 1

How to add a UTF-8 Byte-order-marker to a file?

Posted: 22 Jul 2015 06:20
by avsfan
Hi,

I've got some files that are UTF-8 encoded, but without the byte-order-marker. I know there must be a way to add the needed characters to the beginning of these files, but I'm not sure how to do it.

Any ideas?

Thanks!

andy

Re: How to add a UTF-8 Byte-order-marker to a file?

Posted: 22 Jul 2015 15:45
by bdeshi
So, did I just discover that there's no easy way for reading/writing raw bytes in XY?

this a workaround:

Code: Select all

"add bom to file"
 $file = "UTF8_file_without_BOM.txt";
 writefile($file, hexdump('EFBBBF',2,'ri').readfile($file,'b'), , 'b');
WARNING: this doesn't check if the input file is UTF8-encoded, already has BOM, or even if it is a textfile at all. This just blindly adds the BOM characters to the beginning of the input file.

Re: How to add a UTF-8 Byte-order-marker to a file?

Posted: 24 Jul 2015 23:44
by avsfan
Thanks, Sammay! Works great!