Home > Article > Backend Development > The role of PHP encoding conversion in Excel reading_PHP tutorial
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 If so, you can consider using excel_class.php. If you google it, you can find the source code of this class to download, and 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 what is 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];
to display the content in row 1 and column 1. The original content is " Start", it is indeed "Start" when displayed on the web using PHP, but the source code of viewing the web page is
开始
where is for displaying on the web page It shows that the hexadecimal representation of 24320 and 22987 is the "start" UTF-16LE encoding.
Then what we need to do is 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;
}
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');
At this point, UTF-16LE is completed UTF-8 PHP encoding conversion.