PHP detection file encoding method, _PHP tutorial
PHP detection file encoding method,
Regarding file encoding detection, Baidu has a lot of them, but there is really no useful one.
Many people suggest mb_detect_encoding detection , but for some reason I didn’t succeed, and nothing was output.
I saw someone wrote an enhanced version, which was judged by BOM, so I ignored it decisively. This thing is completely unreliable.
Finally, according to PHP In the example below the mb_detect_encoding function in the manual, I wrote a detection function myself.
It also includes a function that automatically detects the encoding and reads the file according to the pointed encoding.
The source code is provided. If you don’t like it, 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 criticize me, I’m just sharing my thoughts,,
<span class="hljs-preprocessor"><?php <span class="hljs-comment">/** * 检测文件编码 *<span class="hljs-phpdoc"> @param string $file 文件路径 *<span class="hljs-phpdoc"> @return string|null 返回 编码名 或 null */ <span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">detect_encoding<span class="hljs-params">(<span class="hljs-variable">$file) { <span class="hljs-variable">$list = <span class="hljs-keyword">array(<span class="hljs-string">'GBK', <span class="hljs-string">'UTF-8', <span class="hljs-string">'UTF-16LE', <span class="hljs-string">'UTF-16BE', <span class="hljs-string">'ISO-8859-1'); <span class="hljs-variable">$str = file_get_contents(<span class="hljs-variable">$file); <span class="hljs-keyword">foreach (<span class="hljs-variable">$list <span class="hljs-keyword">as <span class="hljs-variable">$item) { <span class="hljs-variable">$tmp = mb_convert_encoding(<span class="hljs-variable">$str, <span class="hljs-variable">$item, <span class="hljs-variable">$item); <span class="hljs-keyword">if (md5(<span class="hljs-variable">$tmp) == md5(<span class="hljs-variable">$str)) { <span class="hljs-keyword">return <span class="hljs-variable">$item; } } <span class="hljs-keyword">return <span class="hljs-keyword">null; } <span class="hljs-comment">/** * 自动解析编码读入文件 *<span class="hljs-phpdoc"> @param string $file 文件路径 *<span class="hljs-phpdoc"> @param string $charset 读取编码 *<span class="hljs-phpdoc"> @return string 返回读取内容 */ <span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">auto_read<span class="hljs-params">(<span class="hljs-variable">$file, <span class="hljs-variable">$charset=<span class="hljs-string">'UTF-8') { <span class="hljs-variable">$list = <span class="hljs-keyword">array(<span class="hljs-string">'GBK', <span class="hljs-string">'UTF-8', <span class="hljs-string">'UTF-16LE', <span class="hljs-string">'UTF-16BE', <span class="hljs-string">'ISO-8859-1'); <span class="hljs-variable">$str = file_get_contents(<span class="hljs-variable">$file); <span class="hljs-keyword">foreach (<span class="hljs-variable">$list <span class="hljs-keyword">as <span class="hljs-variable">$item) { <span class="hljs-variable">$tmp = mb_convert_encoding(<span class="hljs-variable">$str, <span class="hljs-variable">$item, <span class="hljs-variable">$item); <span class="hljs-keyword">if (md5(<span class="hljs-variable">$tmp) == md5(<span class="hljs-variable">$str)) { <span class="hljs-keyword">return mb_convert_encoding(<span class="hljs-variable">$str, <span class="hljs-variable">$charset, <span class="hljs-variable">$item); } } <span class="hljs-keyword">return <span class="hljs-string">""; }</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

This article introduces six popular AI tools, including Douyin Doubao, Wenxin Yige, Tencent Zhiying, Baidu Feipiao EasyDL, Baidu AI Studio and iFlytek Spark Cognitive Large Model. These tools cover different functions such as text creation, image generation, video editing, and AI model development. Choosing the right AI tool requires consideration of factors such as functional requirements, technical level, and cost budget. These tools provide convenient and efficient solutions for individuals and businesses in need of AI assistance.

The following resources contain additional information on CakePHP. Please use them to get more in-depth knowledge on this.
