Home php教程 php手册 PHP自动识别字符集编码并完成转码

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

May 25, 2016 pm 04:44 PM
character set coding Automatic Identification

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

在PHP处理页面的时候,我们对于字符集的转换都是采用了iconv或者mb_convert等函数,但这其实是有一个前提的,即我们事先得知道in和out是什么样的编码,我们才能进行正确的转换.

下面这个函数,就可以在不知道源字符串编码的情况下,自动判断其编码并进行转换,虽然只支持UTF8编码和GB2312编码,但对于国内绝大多数网站来说,已经够用了,代码如下:

<?php
function safeEncoding($string, $outEncoding = &#39;UTF-8&#39;) {
    $encoding = "UTF-8";
    for ($i = 0; $i < 128) 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 return iconv($encoding, $outEncoding, $string);
}
?>
Copy after login

识别汉字编码,因为YBlog用的是utf-8,如果引用通告发过来的是gb2312的编码的话,需要可以识别并完成编码转换,代码如下:

<?php
function safeEncoding($string, $outEncoding = &#39;UTF-8&#39;) {
    $encoding = "UTF-8";
    for ($i = 0; $i < strlen($string); $i++) {
        if (ord($string{$i}) < 128) 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 return iconv($encoding, $outEncoding, $string);
}
?>
Copy after login

               
               

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

Video Face Swap

Video Face Swap

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

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)

11 common classification feature encoding techniques 11 common classification feature encoding techniques Apr 12, 2023 pm 12:16 PM

Machine learning algorithms only accept numerical input, so if we encounter categorical features, we will encode the categorical features. This article summarizes 11 common categorical variable encoding methods. 1. ONE HOT ENCODING The most popular and commonly used encoding method is One Hot Enoding. A single variable with n observations and d distinct values ​​is converted into d binary variables with n observations, each binary variable is identified by a bit (0, 1). For example: the simplest implementation after coding is to use pandas' get_dummiesnew_df=pd.get_dummies(columns=[‘Sex’], data=df)2,

How many bytes do utf8 encoded Chinese characters occupy? How many bytes do utf8 encoded Chinese characters occupy? Feb 21, 2023 am 11:40 AM

UTF8 encoded Chinese characters occupy 3 bytes. In UTF-8 encoding, one Chinese character is equal to three bytes, and one Chinese punctuation mark occupies three bytes; while in Unicode encoding, one Chinese character (including traditional Chinese) is equal to two bytes. UTF-8 uses 1~4 bytes to encode each character. One US-ASCIl character only needs 1 byte to encode. Latin, Greek, Cyrillic, Armenian, and Hebrew with diacritical marks. , Arabic, Syriac and other letters require 2-byte encoding.

Knowledge graph: the ideal partner for large models Knowledge graph: the ideal partner for large models Jan 29, 2024 am 09:21 AM

Large language models (LLMs) have the ability to generate smooth and coherent text, bringing new prospects to areas such as artificial intelligence conversation and creative writing. However, LLM also has some key limitations. First, their knowledge is limited to patterns recognized from training data, lacking a true understanding of the world. Second, reasoning skills are limited and cannot make logical inferences or fuse facts from multiple data sources. When faced with more complex and open-ended questions, LLM's answers may become absurd or contradictory, known as "illusions." Therefore, although LLM is very useful in some aspects, it still has certain limitations when dealing with complex problems and real-world situations. In order to bridge these gaps, retrieval-augmented generation (RAG) systems have emerged in recent years. The core idea is

Several common encoding methods Several common encoding methods Oct 24, 2023 am 10:09 AM

Common encoding methods include ASCII encoding, Unicode encoding, UTF-8 encoding, UTF-16 encoding, GBK encoding, etc. Detailed introduction: 1. ASCII encoding is the earliest character encoding standard, using 7-bit binary numbers to represent 128 characters, including English letters, numbers, punctuation marks, control characters, etc.; 2. Unicode encoding is a method used to represent all characters in the world The standard encoding method of characters, which assigns a unique digital code point to each character; 3. UTF-8 encoding, etc.

PHP coding tips: How to generate a QR code with anti-counterfeiting verification function? PHP coding tips: How to generate a QR code with anti-counterfeiting verification function? Aug 17, 2023 pm 02:42 PM

PHP coding tips: How to generate a QR code with anti-counterfeiting verification function? With the development of e-commerce and the Internet, QR codes are increasingly used in various industries. In the process of using QR codes, in order to ensure product safety and prevent counterfeiting, it is very important to add anti-counterfeiting verification functions to the QR codes. This article will introduce how to use PHP to generate a QR code with anti-counterfeiting verification function, and attach corresponding code examples. Before starting, we need to prepare the following necessary tools and libraries: PHPQRCode: PHP

Detailed explanation of how to modify the character set of Oracle database Detailed explanation of how to modify the character set of Oracle database Mar 02, 2024 pm 03:18 PM

Detailed explanation of how to modify the character set of Oracle database Oracle database is a powerful relational database management system that supports multiple character sets, including Simplified Chinese character set, Traditional Chinese character set, English character set, etc. In practical applications, you may encounter situations where you need to modify the database character set. This article will introduce in detail the method of modifying the Oracle database character set and provide specific code examples for readers' reference. 1. Check the current database character set. Before modifying the database character set, you first need to check the current database character set.

What are the hdb3 encoding rules? What are the hdb3 encoding rules? Aug 29, 2023 pm 01:38 PM

The coding rules are: 1. If the previous code is 0 and the current data bit is 0, the code is 0; 2. If the previous code is 0 and the current data bit is 1, the code is bipolar pulse (+A or - A), and the counter is increased by 1; 3. If the previous code is 1 and the current data bit is 1, the code is 0, and the counter is increased by 1; 4. If the previous code is 1, the current data bit is 0, The encoding method is determined based on the parity of the counter. If it is an even number, the encoding is (+B or -B). If it is an odd number, the encoding is zero level and the counter is cleared and so on.

How to solve the problem of encoding of php database query results How to solve the problem of encoding of php database query results Mar 21, 2023 am 11:49 AM

PHP is a popular web programming language that can be used to write dynamic web pages and applications. In practical applications, PHP often needs to interact with the database to query and process data. However, when using PHP to get results from a database, you may encounter encoding problems, which often result in garbled characters. So, how to solve the problem of encoding of PHP database query results?

See all articles