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

Discuss and share scripts and script files...
Post Reply
avsfan
Posts: 554
Joined: 29 Jun 2006 09:00
Location: Fort Collins, Colorado

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

Post 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

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

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

Post 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.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

avsfan
Posts: 554
Joined: 29 Jun 2006 09:00
Location: Fort Collins, Colorado

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

Post by avsfan »

Thanks, Sammay! Works great!

Post Reply