Example of creating a UTF-8 encoded file using php fopen

Source: Internet
Author: User
Tags fopen example mongodb php file php code postgresql redis

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".

Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

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.