Method for php to create utf8 encoded file using fopen

  • 2021-07-22 09:20:32
  • OfStack

This article illustrates how php uses fopen to create an utf8 encoded file. Share it for your reference. The specific implementation method is as follows:

Generally speaking, if we use fopen directly to create files, we will find that the encoding of files is not uft-8, so if we want to create uft8 files, we need to do 1 some technical processing. The specific steps are as follows:

Using PHP to create a file encoded in utf-8 format:

Step 1: Create a new txt file, open it, file- > Save as xxx. php and change the encoding to UTF-8. Save.

Part 2: Add the following code to the php file:

<?php
$filename=rand(100,999).".txt";// Define the file name and file format to be created (change it as needed)
$str = "PHP Learning network [www.ofstack.com]";// Contents to be written to the new file
if (!$head=fopen($filename, "w+")) {// Open the file read-write, point the file pointer to the header and cut the file size to zero. If the file does not exist, it will be automatically created
die(" Try to open a file [".$filename."] Failure ! Please check that you have sufficient permissions ! Creation process terminated !");
}
if (fwrite($head,$str)==false) {// Perform a write to a file
fclose($head);
die(" Failed to write content ! Please check that you have sufficient permissions ! Write process terminated !");
}
echo " Successfully created UTF-8 Format file [".$filename."] And writes the contents to the file: ".$str;
fclose($head);
?>

Use this method to create the key points of UTF-8 encoded files:

Ensure that the encoding format of the PHP code file itself is UTF-8
② What is the encoding format of the php code file? What is the file encoding created
③ The problem of garbled code is displayed

There are three main factors that control the display of the page:

1. HTML code control: The head tag in the standard HTML web page file contains this code < meta http-equiv="Content-Type" content="text/html; charset=utf-8" / > The sentence charset=utf-8 in the code tells the browser to display the contents of the web page in utf-8 format.

2. PHP code control: If header ("content-Type: text/html; charset=utf-8 "); This code is also intended to tell the browser to display the contents of the web page in utf-8 format. (Note: This code cannot be preceded by output similar to echo.)

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

Add an example of fopen

<?php
$f=fopen("test.txt", "wb");
$text=utf8_encode("a!");
 
// First use function utf8_encode Change the data to be written to UTF Encoding format.
 
$text="\xEF\xBB\xBF".$text;
 
//"\xEF\xBB\xBF", This string of characters is indispensable, and the generated file will become UTF-8 Format, otherwise it will still be 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 appear garbled phenomenon. After 1 debugging, the code is as follows:

<?php
$ctxtsubmit=" Good ";
$f=fopen("../".$file, "wb");
    //$text=utf8_encode($ctxtsubmit);
    // First use function utf8_encode Change the data to be written to UTF Encoding format.
    $text="\xEF\xBB\xBF".$ctxtsubmit;
     //"\xEF\xBB\xBF", This string of characters is indispensable, and the generated file will become UTF-8 Format, otherwise it will still be ANSI Format.
    fputs($f, $text);
    // Write.
     fclose($f);
?>

I hope this article is helpful to everyone's PHP programming.


Related articles: