search
Homephp教程php手册PHP自动识别字符集并完成转码详解

PHP自动识别字符集并完成转码详解

Jun 13, 2016 am 11:42 AM
phputf-8generallyusecharactercharacter setFinishyesofcodingAutomatic IdentificationOwnDetailed explanationTranscode

因为自己使用字符编码一般的是utf-8编码,但如果对方的博客使用gb2312编码的话,POST过来就会出现乱码(除非对方POST前先转换编码)。在不能保证对方是否一定使用utf-8编码的情况下,自己做一个编码的检查和转换是很有必要的。

写了个函数来完成这个工作,原理很简单,因为gb2312/gbk是中文两字节,这两个字节是有取值范围的,而utf-8中汉字是三字节,同样每个字节也有取值范围。而英文不管在何种编码情况下,都是小于128,只占用一个字节(全角除外)。

如果是文件形式的编码检查,还可以直接check utf-8的BOM信息,关于这方面的东西,大家可以看看TP工具箱的编码转换功能,我在那个AppCodingSwitch类中写了比较详细的注释。

话不多说,直接上函数,这个函数是用来对字符串进行检查和转码的。文件的检查与转码

复制代码 代码如下:


function safeEncoding($string, $outEncoding = 'UTF-8') {
    $encoding = "UTF-8";
    for ($i = 0; $i         if (ord($string{$i})             continue;

        if ((ord($string{$i}) & 224) == 224) {
            //第一个字节判断通过
            $char = $string{++$i};
            if ((ord($char) & 128) == 128) {
                //第二个字节判断通过
                $char = $string{++$i};
                if ((ord($char) & 128) == 128) {
                    $encoding = "UTF-8";
                    break;
                }
            }
        }
        if ((ord($string{$i}) & 192) == 192) {
            //第一个字节判断通过
            $char = $string{++$i};
            if ((ord($char) & 128) == 128) {
                //第二个字节判断通过
                $encoding = "GB2312";
                break;
            }
        }
    }

    if (strtoupper($encoding) == strtoupper($outEncoding))
        return $string;
    else
        returniconv($encoding, $outEncoding, $string);
}


Statement
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software