Php uses fopen to create utf8 encoded files

Source: Internet
Author: User
Tags fopen example
This article mainly introduces how to use fopen to create utf8 encoded files in php. It is a technique that involves coding. For more information, see

This article mainly introduces how to use fopen to create utf8 encoded files in php. It is a technique that involves coding. For more information, see

This example describes how to create a UTF-8 encoded file using fopen in php. Share it with you for your reference. The specific implementation method is as follows:

In general, if we directly use fopen to create will find that the file encoding is not a uft-8, then if you want to create a uft8 file we need some technical processing. The procedure is as follows:

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 learning network []"; // 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 );
?>


Key points for creating a UTF-8 encoding file using this method:

Ensure that the PHP code file encoding format is UTF-8
② What is the encoding format of php code files? What is the file encoding created?
③ Display garbled characters

The control page displays three main factors:

1. HTML code control: the head label in the standard HTML webpage file contains this code. 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:

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 hope this article will help you with PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.