Home > Backend Development > PHP Tutorial > How is PHP encoding converted in excel files? How to use php encoding conversion

How is PHP encoding converted in excel files? How to use php encoding conversion

不言
Release: 2023-04-03 13:02:02
Original
1664 people have browsed it

PHP encoding conversion is a relatively basic knowledge point in PHP, but for many friends who have just started learning PHP, they may not care much about its importance. Next, let’s talk in detail about the role of PHP encoding conversion in Excel reading. I hope you can have a good grasp of it.

PHP has developed a lot. Now pear is very convenient to use. There are related classes to read the contents of Excel files. If you don’t want to use pear, you can consider using excel_class.php and Google it. You can find the source of this class When downloading the code, you can also find the basic example code, which is very convenient to use.

What you need to pay attention to when converting PHP encoding is that the data read from Excel belongs to UTF-16LE encoding. If you use excel_class in mobile applications, you need to pay attention because mobile phones usually support UTF-8. Coding, which involves the conversion of codes.

For example, I use

echo $return[Sheet2][0][0];
Copy after login

to display the content in row 1 and column 1. The original content is "Start", and when displayed on the web using PHP, it is indeed "Start". But the source code of viewing the web page is

开始

, where &# is for display on the web page, and the hexadecimal representation of 24320 and 22987 is the "start" UTF -16LE encoding.

Then what we need to do is to convert this UTF-16LE encoding to UTF-8 encoding.
First open excel_class.php, find the function uc2html, comment out the code in the function, and return the parameters directly, that is, changing the function does not perform any operation.

function uc2html($str) {
return $str;
}
Copy after login

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

echo mb_convert_encoding($return[Sheet2][0][0], 'UTF-8', 'UTF-16LE');
Copy after login

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

Recommended related articles:

How to solve php encoding conversion garbled characters

php character encoding conversion

The above is the detailed content of How is PHP encoding converted in excel files? How to use php encoding conversion. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template