Use PHP to create a UTF-8 file:
Step 1: Create a txt file, open, File-> save as xxx. php, and change the encoding to UTF-8, save.
Part 2: Add the following code to the PHP file:
The code is as follows:
<? Php
$ Filename = rand (100,999). ". txt"; // define the name and format of the file to be created (change as needed)
$ Str = "PHP well-known network [www.111cn.net]"; // content in the new file to be written
If (! $ Head = fopen ($ filename, "w +") {// open the file in read/write mode, point the file pointer to the file header, and cut the file size to zero, automatic creation if the file does not exist
Die ("trying to open the file [". $ filename. "] failed! Check whether you have sufficient permissions! The creation process is terminated! ");
}
If (fwrite ($ head, $ str) = false) {// execute the write operation
Fclose ($ head );
Die ("writing content failed! Check whether you have sufficient permissions! The write process is terminated! ");
}
Echo "successfully created the UTF-8 format file [". $ filename. "] and written to the File:". $ str;
Fclose ($ head );
?>
Ensure that the encoding format of the PHP code file itself is UTF-8Key points for creating a UTF-8 encoding file using this method:
What is the encoding format of php code files? What is the file encoding created?
Displays garbled characters.
The control page displays three main factors:
1. HTML code control: the head label in the standard HTML webpage file contains the code <meta http-equiv = "Content-Type" content = "text/html; charset = utf-8 "/> in the code, charset = utf-8 tells the browser to display the content in the webpage in UTF-8 format.
2. PHP code control: if header ("content-Type: text/html; charset = utf-8") is added at the beginning of the PHP file, it is also used to tell the browser to display the content in the webpage in UTF-8 format. (Note: No echo-like output can be provided before this code)
3. File physical storage property control: open a file in notepad, and save the file as. What you see in "encoding" is the real encoding of the current file.
Add a fopen example
The code is as follows:
<? Php
$ F = fopen ("test.txt", "wb ");
$ Text = utf8_encode ("! ");
// Use the utf8_encode function to convert the data to the UTF encoding format.
$ Text = "\ xEF \ xBB \ xBF". $ text;
// "\ XEF \ xBB \ xBF", this string of characters is indispensable, the generated file will be in UTF-8 format, otherwise it will still be in ANSI format.
Fputs ($ f, $ text );
// Write.
Fclose ($ f );
?>
The file encoding format created in this way is indeed UTF-8, but the Chinese characters in the file are garbled. After some debugging,
The code is as follows:
<? Php $ Ctxtsubmit = "OK ";
$ F = fopen ("../". $ file, "wb ");
// $ Text = utf8_encode ($ ctxtsubmit );
// Use the utf8_encode function to convert the data to the UTF encoding format.
$ Text = "\ xEF \ xBB \ xBF". $ ctxtsubmit;
// "\ XEF \ xBB \ xBF", this string of characters is indispensable, the generated file will be in UTF-8 format, otherwise it will still be in ANSI format.
Fputs ($ f, $ text );
// Write.
Fclose ($ f );
?>
I do not understand why this string is added to "\ xEF \ xBB \ xBF".