Table of Contents
PHP detection file encoding method,
Home Backend Development PHP Tutorial PHP detection file encoding method, _PHP tutorial

PHP detection file encoding method, _PHP tutorial

Jul 12, 2016 am 09:01 AM
php one but about document method have Detection Baidu of coding

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,,

text
<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>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1087779.htmlTechArticlephp detection file encoding method. Regarding the detection of file encoding, Baidu has a lot of them, but there is really no way. Many people recommend mb_detect_encoding detection, but I don’t know why...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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 web version entrance deepseek official website entrance deepseek web version entrance deepseek official website entrance Feb 19, 2025 pm 04:54 PM

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.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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 Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

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.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

ai tool recommendation ai tool recommendation Nov 29, 2024 am 11:08 AM

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.

CakePHP Useful Resources CakePHP Useful Resources Sep 10, 2024 pm 05:27 PM

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

See all articles