Home > Backend Development > PHP Tutorial > I wrote a PHP function to detect file encoding_PHP tutorial

I wrote a PHP function to detect file encoding_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:32:03
Original
757 people have browsed it

Regarding file encoding detection, there are a lot of them on Baidu, but there is really nothing useful.
Many people suggested mb_detect_encoding detection, but for some reason I failed and nothing was output.
I saw someone write I bought an enhanced version and used BOM to judge, but I decisively ignored it. This thing is completely unreliable.
Finally, I wrote a detection function based on the example below the mb_detect_encoding function in the PHP manual.
It also includes automatic The function that detects the encoding and reads the file according to the specified encoding.
The source code is provided. If you don’t like it, please don’t comment.
I wrote this after trying the online method but it didn’t work. Maybe it’s caused by the different environment.
So if it doesn’t work, don’t blame me, I’m just sharing my thoughts.

Copy the code The code is as follows:

/**
* Detect file encoding
* @param string $file file path
* @return string|null return encoding name or null
*/
function detect_encoding($file) {
$list = array('GBK', 'UTF-8', 'UTF- 16LE', 'UTF-16BE', 'ISO-8859-1');
$str = file_get_contents($file);
foreach ($list as $item) {
$tmp = mb_convert_encoding( $str, $item, $item);
if (md5($tmp) == md5($str)) {
return $item;
}
}
return null;
}

/**
* Automatically parse the encoding and read the file
* @param string $file file path
* @param string $charset read the encoding
* @return string return the read content
*/
function auto_read($file, $charset='UTF-8') {
$list = array('GBK', ' UTF-8', 'UTF-16LE', 'UTF-16BE', 'ISO-8859-1');
$str = file_get_contents($file);
foreach ($list as $item) {
$tmp = mb_convert_encoding($str, $item, $item);
if (md5($tmp) == md5($str)) {
return mb_convert_encoding($str, $charset, $ item);
}
}
return "";
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/756989.htmlTechArticle Regarding the detection of file encoding, Baidu has a lot of them, but there is really nothing that can be used. Many people have suggested mb_detect_encoding detection, but for some reason I failed, nothing...
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template