PHP encoding conversion in Excel reading

Source: Internet
Author: User

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.

In PHP encoding conversion need to pay attention to is, read from the Excel is a UTF-16LE encoding, if the use of excel_class in mobile applications, you need to pay attention to, 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, PHP Code Conversion from UTF-16LE to UTF-8 is completed.


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.