Home > Backend Development > PHP Tutorial > How to Remove Byte Order Marks (BOMs) from a UTF-8 File?

How to Remove Byte Order Marks (BOMs) from a UTF-8 File?

Linda Hamilton
Release: 2024-12-17 10:28:24
Original
943 people have browsed it

How to Remove Byte Order Marks (BOMs) from a UTF-8 File?

How to Remove Byte Order Marks (BOMs) from the Beginning of a File

Problem:

You encounter an issue with a CSS file containing invisible characters, denoted by , that disrupt its proper functioning when read by PHP. The file is saved in UTF-8 encoding, but removing the characters manually is challenging.

Answer:

The characters you are observing are known as Byte Order Marks (BOMs). A BOM is a Unicode character that indicates the byte order (endianness) of a file. In this case, the UTF-8 BOM signifies that the file uses UTF-8 encoding.

Solutions:

  • Disable BOMs in Editor: Configure your editor to not insert BOMs when saving files. In some editors, you may find a setting related to "BOM."
  • Strip BOMs with awk: Use the awk command-line utility to remove BOMs from the file:
awk 'sub(/^\xEF\xBB\xBF/, "")' input.css > output.css
Copy after login
  • Handle BOMs in PHP: Set the PHP internal encoding to UTF-8 to ignore BOMs when reading files:
<?php
mb_internal_encoding('UTF-8');
// Read and process CSS files
mb_internal_encoding('previous encoding'); // Restore previous encoding
?>
Copy after login

Note:

  • ISO-8859-15 encoding cannot represent the UTF-8 BOM, so saving the file in that format will result in data loss.
  • Line endings (Windows vs. Linux) should not affect BOM detection.

The above is the detailed content of How to Remove Byte Order Marks (BOMs) from a UTF-8 File?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template