How to convert data from GB2312 to UTF-8

Source: Internet
Author: User
How to convert data from GB2312 to UTF-8 I tried both iconv and mb_convert_encoding, and then found that the results are the same, there will be various problems, it's a direct error. how can this problem be solved?


Reply to discussion (solution)

Iconv ("GB2312", "UTF-8 // IGNORE", $ data)

Are you sure the original data is GB2312 encoded?

Yes. it is in ANSI format.

Yes. it is in ANSI format.
What is the encoding of your file page? Do you want to paste the code out?

Yes. in this case, the import function of a scv file is implemented, and the storage code is ANSI in Notepad. now, the fgetcvs is used for loop, which is not normal. Previously, it was okay to use notepad to open and save the function as UTF-8 import, so the page encoding should be UTF-8

GB2312 is a subset of GBK.
UCS-2 is a variant of GBK
Simple displacement operation between UCS-2 and UTF-8

Therefore, there is no data loss to convert from GB2312 or GBK to UTF-8.
In turn, there is a possibility of loss.

ANSI cannot describe any problems
ANSI refers to the national standard, and the computer refers to the default character set of the operating system (which can be modified manually)
Take windows as an example. the simplified Chinese system is GBK and the traditional Chinese system is BIG5.

Can you see the region information in the cvs file I filled in is Hubei province, Hunan province, Guangdong province, through iconv ('gb2312', 'utf-8', $ str ), it is converted into a "province". if Shandong province or Shanxi province is a complete translation, Sichuan province is translated into a "". what is the reason? it has been stuck for two days.

Can you put your files on a cloud disk?
Or
Echo base64_encode (file_get_contents ('your csv filename ', false, null, 0, 1000 ));
Post result

The result is as follows:


Then I use echo iconv ('gbk', 'utf-8', file_get_contents ($ file ['tmp _ name']); return false;
All the results are utf8 data. Unfortunately, this is not what I want.

1. paste base64 data
2. what are you going to do?

Paste the code to see a brief description of your needs

Export/export + 7W1b/Nu6c6Miy0 + export/zbunOjEsveHL47/Nu6c6MizP + srbv827pzozo6kiLMv5yvS/zbunLMv51NrH + NPyLMGqz7XIyyzBqs + export =
All I want is to use the fgetcsv function to loop through each row of Records to form an array and store the records to the database. However, if file_get_contents is used directly, it is difficult to achieve this because there are many fields in the cvs file, some of them are optional. So tangled

What is the relationship between the encoding and conversion?

$ Fp = fopen ('your csv filename ', 'r'); while ($ row = fgetcsv ($ fp) {$ res [] = $ row ;} print_r ($ res );

Thanks to the moderator for helping solve the problem so late, the cvs file encoding is gb2312 and the database encoding is utf8, then, before I load the array, I need to compare the data obtained here with the database to obtain the data I want. In addition, many fields in the cvs file contain Chinese characters, therefore, transcoding is required before it can be imported into the database. The problem is that after file_get_contents is external, transcoding can be performed one by one in the loop.

Transcoding not required
You only need to execute mysql_query ('set names gbk') once before the comparison.

The moderator asked another question. how can I import the gbk-encoded data into the database? utf8 is used in the database. I doubt whether it is the function of fegtcvs, in this case, transcoding cannot be performed outside.

You only need to execute mysql_query ('set names gbk') before inserting the gbk data into the utf8 table.

Mysql_query ('set names gbk ');
The purpose of this command is to notify mysql to use the gbk character set for operations.
Transcoding is completed by mysql itself.

Thanks to the moderator. although I changed file to replace fgetcsv

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.