


PHP automatically obtains the string encoding function mb_detect_encoding_PHP tutorial
When using the mb_detect_encoding function in php for encoding identification, many people have encountered the problem of incorrect encoding, such as GB2312 and UTF-8, or UTF-8 and GBK (here mainly for cp936) Judgment), it is said on the Internet that because the characters are short, mb_detect_encoding will misjudge.
For example:
$encode = mb_detect_encoding($keytitle, array("ASCII" ,'UTF-8′,"GB2312′,"GBK",'BIG5′));
if ($encode == “UTF-8″){
$keytitle = iconv("UTF-8″ ,"GBK",$keytitle);
}
The function of this code is to detect whether the encoding of the string is UTF-8, and if so, convert it to GBK. But when $keytitle = "%D0%BE%C6%AC";. The detection result is UTF-8. This bug is not actually a bug, and you should not rely too much on mb_detect_encoding when writing programs. When the string is shorter, the detection result The possibility of deviation is very high.
How to solve it? My solution is:
adjusts the order of encoding detection to put the greatest possibility in the front, thus reducing the chance of incorrect conversion
General. To sort gb2312 first, when there are GBK and UTF-8, you need to sort the commonly used ones to the front
.

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 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
