What are the methods for php to solve garbled characters written into txt?

尊渡假赌尊渡假赌尊渡假赌
Release: 2023-06-06 13:41:23
Original
1384 people have browsed it

php There are three ways to solve garbled characters when writing txt: 1. Set the file encoding. Before writing the file, you can set the file encoding to UTF-8; 2. Output the BOM byte order mark to identify which Unicode encoding method the file uses to prevent garbled characters; 3. Use the fwrite() function with options to ensure that the data length written to the file is consistent with the provided The same length prevents garbled characters.

What are the methods for php to solve garbled characters written into txt?

Operating system for this tutorial: Windows 10 system, php8.1.3 version, Dell G3 computer.

Solution to php writing garbled txt characters:

1. Set the file encoding

Before writing the file, you can call mb_internal_encoding() and mb_http_output() function, set the file encoding to UTF-8.

<?php
// 设置内部字符编码为UTF-8
mb_internal_encoding(&#39;UTF-8&#39;);
// 设置 HTTP 输出字符编码为UTF-8
mb_http_output(&#39;UTF-8&#39;);
$file = &#39;data.txt&#39;;
$content = &#39;你好世界!&#39;;
$handle = fopen($file, &#39;w&#39;);
fwrite($handle, $content);
fclose($handle);
?>
Copy after login

2. Output BOM byte order mark

BOM (Byte Order Mark) is a special byte sequence that usually appears at the beginning of a Unicode text file. Use Used to identify which Unicode encoding method (big-endian or little-endian) the file uses. If there is no BOM mark in the file header, the text may be garbled.

You can use the following code to output the BOM mark in the file:

<?php
$file = &#39;data.txt&#39;;
$content = "\xEF\xBB\xBF" . &#39;你好世界!&#39;;  // 在内容前添加BOM标记
$handle = fopen($file, &#39;w&#39;);
fwrite($handle, $content);
fclose($handle);
?>
Copy after login

3. Use the fwrite() function with options

fwrite() The function can accept an optional third parameter, indicating the length of the string to be written. If the length of the data written to the file is inconsistent with the provided length, garbled characters will appear. Therefore, by using this option, you can ensure that the data written to the file is of the same length as the supplied length.

<?php
$file = &#39;data.txt&#39;;
$content = &#39;你好世界!&#39;;
$handle = fopen($file, &#39;w&#39;);
$length = strlen($content);
fwrite($handle, $content, $length);
fclose($handle);
?>
Copy after login

The above is the detailed content of What are the methods for php to solve garbled characters written into txt?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template