How to convert the GB encoding to UTF-8?

Source: Internet
Author: User
I tried to use the getid3 class library to extract the song information from mp3 files. most Chinese songs are normal, but a few files are garbled. // & Nbsp; this is the extracted character and its base64 encoded var_dump ($ str, & nbsp; base64_encode ($ str); // & nbsp; how to convert UTF-8 from a GB encoded string
I tried to use the getid3 class library to extract song information from mp3 files. most Chinese songs are normal, but a few files are garbled.

// This is the extracted character and its base64 encoding
Var_dump ($ str, base64_encode ($ str ));
// String (16 )"??????? ±"
// String (24) "w5DDhMOLw4bCv8OxwrPCsQ ="

// Check the character encoding. The result is CP936.
Mb_detect_encoding ($ str, 'gbk, gb2312, GB18030, ISO-8859-1, ASCII, UTF-8 ', true)

// I tried to convert the encoding, but the result is still unsatisfactory.
Mb_convert_encoding ($ str, 'utf-8', 'cp936 ')
// String (24) "Lu halogen"

Because CP936 is compatible with gbk and gb2312, the results are the same. UTF-8 encoding:
------ Solution --------------------
You can also write
Echo iconv ('gbk', 'utf-8', iconv ('utf-8', 'latin1', $ s ));

It is easy to determine whether it is a 2-byte UTF-8 encoding.
Echo (ord ($ s {0}) & 0xf0) = 0xc0? 'Yes': 'not ';
------ Solution --------------------
Reference:
I tried to use the getid3 class library to extract song information from mp3 files. most Chinese songs are normal, but a few files are garbled.

// This is the extracted character and its base64 encoding
Var_dump ($ str, base64_encode ($ str ));
// String (16 )"??????? ±"
// String (24) "w5DDhMOLw4bCv8OxwrPCsQ ="

// Check the character encoding. The result is CP936.
Mb_detect_encoding ($ str, 'gbk, gb2312, GB18030, ISO-8859-1, ASCII, UTF-8 ', true)

// I tried to convert the encoding, but the result is still unsatisfactory.
Mb_convert_encoding ($ str, 'utf-8', 'cp936 ')
// String (24) "Lu halogen"

Because CP936 is compatible with gbk and gb2312, the results are the same.

This should require your server to support some encoding before you can perform conversion. if not, you cannot use program conversion.

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.