How to create utf8 encoded files in php using fopen,
The example in this article describes how PHP uses fopen to create a utf8 encoded file. Share it with everyone for your reference. The specific implementation method is as follows:
Generally speaking, if we create it directly using fopen, we will find that the file encoding is not uft-8, so if we want to create a uft8 file, we need to perform some technical processing. The specific steps are as follows:
How to create a utf-8 file using PHP:
Step one: Create a new txt file, open it, File->Save as xxx.php, change the encoding to UTF-8, and save.
Part 2: Add the following code to the php file:
Copy code The code is as follows:
$filename=rand(100,999).".txt"; //Define the file name and file format to be created (change it as needed)
$str = "PHP Learning Network [www.jb51.net]";//Content to be written into the new file
if (!$head=fopen($filename, "w+")) {//Open the file for reading and writing, point the file pointer to the file header and cut the file size to zero. If the file does not exist, it will be automatically created
die("Attempt to open file [".$filename."] failed! Please check whether you have sufficient permissions! The creation process is terminated!");
}
if (fwrite($head,$str)==false) {//Execute writing file
fclose($head);
die("Failed to write content! Please check whether you have sufficient permissions! The writing process is terminated!");
}
echo "The UTF-8 format file [".$filename."] was successfully created and the content was written to the file: ".$str;
fclose($head);
?>
Key points for creating UTF-8 encoded files using this method:
① Ensure that the encoding format of the PHP code file itself is UTF-8
② Whatever the encoding format of the php code file is, what is the encoding of the created file
③ The problem of garbled characters in the display
There are three main factors that control page display:
1. HTML code control: The head tag in the standard HTML web page file contains this code
2.PHP code control: If you add header("content-Type: text/html; charset=utf-8"); at the beginning of the PHP file, this code is also to tell the browser to use utf-8 Format displays the content on the web page. (Note: This code cannot have echo-like output before)
3. File physical storage attribute control: Open a file with Notepad, File->Save As, what you see in "Encoding" is the real encoding of the current file
Added an example of fopen
Copy code The code is as follows:
$f=fopen("test.txt", "wb");
$text=utf8_encode("a!");
//First use the function utf8_encode to convert the data to be written into UTF encoding format.
$text="xEFxBBxBF".$text;
//"xEFxBBxBF", 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 encoding format of the file created in this way is indeed UTF-8, but the Chinese characters placed in the file are garbled. After some debugging, the code is as follows:
Copy code The code is as follows:
$ctxtsubmit="Okay";
$f=fopen("../".$file, "wb");
//$text=utf8_encode($ctxtsubmit);
//First use the function utf8_encode to convert the data to be written into UTF encoding format.
$text="xEFxBBxBF".$ctxtsubmit;
//"xEFxBBxBF", 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 hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/904014.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/904014.htmlTechArticleHow to use fopen to create utf8 encoded files in php. This article describes how to use fopen to create utf8 encoded files in php. Share it with everyone for your reference. The specific implementation method is as follows: 1...