Header Content Type Charset UTF-8 with BOM

21,004

There's absolutely no need to include a BOM in an HTML file. (If it really includes different encodings, then a UTF-8 BOM would be broken, so it sounds like you're a bit confused.)

If you expect the file to be saved to disk and used in places where UTF-8 isn't expected, you could add a meta element to indicate the encoding as well:

<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>

If in fact you are using a different encoding than UTF-8, then rather than a BOM, just change the value in the meta attribute.

If you really must have a BOM in there then you'll need to make sure that it's first thing you output, e.g.

echo "\xEF\xBB\xBF";

See How can I output a UTF-8 CSV in PHP that Excel will read properly? for an example where that actually makes sense.

Share:
21,004
Admin
Author by

Admin

Updated on July 03, 2020

Comments

  • Admin
    Admin almost 4 years

    Im currently making a file that some have to download and upload. The file have som differents encodings and it only works if the file is opened with UTF-8 with BOM.

    Currently im using this simple header header('Content-Type: text/html; charset=utf-8'); but how can i change the settings so file file would be seen as UTF-8 with BOM.

    Hoping for some help. Thanks!

  • Admin
    Admin almost 11 years
    Thanks mate! Good to know that it isnt needed. I will try just using UTF-8.
  • Henrik Erlandsson
    Henrik Erlandsson about 4 years
    Thanks, the hex gave the correct magic spell. Microsoft should fix their apps.