How is the PHP encoding converted in the Excel file? How to use PHP encoding conversion

Source: Internet
Author: User
PHP code conversion is a relatively basic point of knowledge in PHP, but for many people who have just started to learn PHP, it may not be too concerned about its importance. And then we'll come to the concrete talk about the role of PHP encoding transformation in Excel reading. I hope we can have a good grasp of it.

PHP developed a lot, now pear is very convenient to use, which has the relevant class to read the contents of the Excel file, if you do not want to use pear, you can consider using Excel_ Class.php,google, you can find this class source code download, you can also find the basic example code, easy to use.

It is important to note that in PHP encoding conversion, the UTF-16LE encoding that is read from Excel, if you use Excel_class in mobile applications, you need to be aware, because the phone is usually supported UTF-8 encoding, which involves the conversion of the encoding.

For example I use

echo $return [sheet2][0][0];

To show the contents of the 1th column in row 1th, the original content is "start", and it is really "start" when using PHP to display on the web, but the source code for viewing the Web page is

& #24320 & #22987

where & #是为了在网页上显示, hexadecimal representation of 24320 and 22987 is the Utf-16le encoding for "start".

So what we need to do is convert this Utf-16le code to UTF-8 encoding.
First open the excel_class.php, find the function uc2html, the code in the function commented out, directly return the parameters, that is, the function does not do anything.

function uc2html ($str) {return $str;}

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

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

This completes the PHP code conversion from Utf-16le to UTF-8.

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.