How PHP uses fopen to create UTF8 encoded files _php tips

Source: Internet
Author: User
Tags fopen example

This example describes how PHP uses fopen to create UTF8 encoded files. Share to everyone for your reference. The implementation methods are as follows:

In general, if we use fopen directly to create a file encoding that is not uft-8, then we need to do some technical processing to create UFT8 files. The specific steps are as follows:

Use PHP to create the encoding format for utf-8 files:

The first step: Create a TXT file, open, file-> Save as xxx.php, and the encoding to UTF-8, save.

Part II: Add the following code to the PHP file:

Copy Code code as follows:
<?php
$filename =rand (100,999). ". TXT ";//define the file name and file format you want to create (change as needed)
$STR = "PHP Learning network [www.jb51.net]";/to be written to the contents of the new file
if (! $head =fopen ($filename, "w+")) {//Open the file in read-write mode, point the file pointer to the file header and truncate the file size to zero, and automatically create if the file does not exist
Die ("Attempt to open the file [". $filename. "] Failed! Check to see if you have sufficient permissions! Create process terminate! ";
}
if (fwrite ($head, $str) ==false) {//Execute write file
Fclose ($head);
Die (write content failed!) Check to see if you have sufficient permissions! Write process terminated! ";
}
Echo successfully created the UTF-8 format file [. $filename.] and wrote the contents to the file: ". $str;
Fclose ($head);
?>

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

① guarantees that the PHP code file itself is encoded in a UTF-8 format
What is the encoded format of the ②php code file and what is the file encoding created?
③ Display garbled problem

There are three main factors that control the page display:

1.HTML code control: standard HTML Web page file inside the head tag contains this code <meta http-equiv= "Content-type" content= "text/html;" Charset=utf-8 "/>, Charset=utf-8 in the code is to tell the browser to display the contents of the Web page in utf-8 format.

2.PHP code Control: If at the beginning of the php file, add header ("content-type:text/html; Charset=utf-8 "); This code is also to tell the browser to display the contents of the Web page in utf-8 format. (Note: This code cannot have an echo-like output before)

3. File Physical storage attribute control: Open a file with Notepad, file-> Save As, what you see in "code" is the real encoding of the current file

Add a fopen example

Copy Code code as follows:
<?php
$f =fopen ("Test.txt", "WB");
$text =utf8_encode ("a!");

Use function Utf8_encode to convert the data you want to write into a UTF encoding format.

$text = "\XEF\XBB\XBF". $text;

"\XEF\XBB\XBF", this string of characters is indispensable, the generated file will become the UTF-8 format, otherwise it is still ANSI format.

Fputs ($f, $text);

Write.

Fclose ($f);
?>

This creates the file encoding format is indeed utf-8, but put in the file of Chinese characters appear garbled phenomenon, after debugging, the code is as follows:

Copy Code code as follows:
<?php
$ctxtsubmit = "good";
$f =fopen (".. /". $file," WB ");
$text =utf8_encode ($ctxtsubmit);
Use function Utf8_encode to convert the data you want to write into a UTF encoding format.
$text = "\XEF\XBB\XBF". $ctxtsubmit;
"\XEF\XBB\XBF", this string of characters is indispensable, the generated file will become the UTF-8 format, otherwise it is still ANSI format.
Fputs ($f, $text);
Write.
Fclose ($f);
?>

I hope this article will help you with your PHP program design.

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.