How to Save a File in UTF-8 Format with a BOM?

Patricia Arquette
Release: 2024-11-07 15:29:03
Original
765 people have browsed it

How to Save a File in UTF-8 Format with a BOM?

How to Write a File in UTF-8 Format

Problem:

You have non-UTF-8 encoded files and want to convert them to UTF-8 without disrupting their content. However, your script using standard file manipulation functions saves the files in the original encoding.

Solution:

To save files in UTF-8 format, you can use file_put_contents(). Additionally, to ensure that the file is interpreted correctly by various applications, add a Byte Order Mark (BOM) to the beginning of the file.

Code:

file_put_contents($myFile, "\xEF\xBB\xBF".  $content);
Copy after login

Explanation:

  • xEFxBBxBF represents the BOM for UTF-8 in hexadecimal format.
  • $content is the data you want to save in the file.

The above is the detailed content of How to Save a File in UTF-8 Format with a BOM?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!