PHP parsing Excel stored in MySQL garbled characters

Source: Internet
Author: User
Tags pear

Encoding is a basic issue that is easily overlooked by everyone. before writing code, you should consider it a lot and it will be less troublesome in the future.

PHP has developed a lot, And now pear is very easy to use. There are related classes to read the content in the Excel file. If you don't want to use pear, you can consider using excel_class.php and Google it, you can find the source code download for this class, and also find the basic example code, which is very convenient to use.

It should be noted that the read from the Excel is a UTF-16LE code, if the use of excel_class in mobile applications, You Need To note, because the phone is usually support UTF-8 encoding, encoding conversion is involved.

For example
Echo $ return [sheet2] [0] [0];
To display the content in the 1st rows and 1st columns. The original content is "start". When PHP is used to display the content on the web, it is indeed "start", but the source code of viewing the web page is
#24320 & #22987
Where & # is to display on the web page, the hexadecimal representation of 24320 and 22987 is the UTF-16LE code of "start.

So what we need to do is to convert this UTF-16LE code into a UTF-8 code.
First, open excel_class.php, find the uc2html function, comment out the code in the function, and directly return the parameter, that is, the function does not perform any operation.
Function uc2html ($ Str ){
Return $ STR;
}

Next, use the function mb_convert_encoding provided in PHP to convert the UTF-16LE to a UTF-8.

Echo mb_convert_encoding ($ return [sheet2] [0] [0], 'utf-8', 'utf-16le ');

At this point, the conversion from UTF-16LE to UTF-8 is completed.

 

 

It should be noted that the read from the Excel is a UTF-16LE code, if the use of excel_class in mobile applications, You Need To note, because the phone is usually support UTF-8 encoding, encoding conversion is involved. <Br/> for example, you can use <br/> echo $ return [sheet2] [0] [0]; <br/> to display the content in column 1st of row 1st, the original content is "START". It is "START" when PHP is displayed on the web ", however, the source code of the web page is <br/> started <br/> where & # Is to be displayed on the web page, the hexadecimal representation of 24320 and 22987 is the UTF-16LE code of "start. <Br/> what we need to do is to convert this UTF-16LE code into a UTF-8 code. <Br/> first, open excel_class.php, find the uc2html function, comment out the code in the function, and return the parameter directly. That is, the function is modified without any operation. <Br/> function uc2html ($ Str) {<br/> return $ STR; <br/>}< br/> Unicode is "UTF-16LE", which I can use: <br/> mb_convert_encoding ($ STR, "UTF-16LE", "UTF-8") <br/> or <br/> iconv ("UTF-8", "UTF-16LE", $ Str) <br/> convert the UTF-8 to Unicode. <Br/> 

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.