Home Backend Development PHP Tutorial How to solve php encoding conversion garbled characters

How to solve php encoding conversion garbled characters

Mar 28, 2018 pm 03:29 PM
php how solve

This article mainly shares with you how to solve php encoding conversion garbled code, combining text and code, I hope it can help everyone.

iconv Detailed explanation:
iconv - String is converted according to the required character encoding
iconv has a bug, and it will not be able to convert some rare characters. Of course, when configuring the second parameter, you can make up for the default defects a little, so that it will not be unable to convert or truncate. The usage is as follows
iconv("UTF-8″, "GB2312//IGNORE",$data);
When encountering a rare word conversion failure, it will ignore the failure and continue to convert the following content.

iconvstring iconv ( string $in_charset , string $out_charset , string $str )

第一个参数:内容原的编码

第二个参数:目标编码

第三个参数:要转的字符串

函数返回字符串<?php$instr = ‘测试’;// GBK转UTF-8$outstr = iconv(‘GBK’,&#39;UTF-8′,$instr);

?>
Copy after login

Return value
Returns the converted string, or returns FALSE on failure.

mb_convert_encoding detailed explanation:
In order to ensure the success rate of conversion, we can use another conversion function
mb_convert_encoding, this The efficiency of the function is not very high. In addition, this function can also omit the third parameter and automatically identify the content encoding. However, it is best not to use it, which will affect the efficiency. You also need to pay attention to the fact that the order of mb_convert_encoding and iconv parameters is different, so be sure to pay attention.

Attached are the simple usage of two functions:

mb_convert_encoding
string mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding ] )
第一个参数:要处理的字符串
第二个参数:目标编码
第三个参数:内容原编码,它可以是一个 array 也可以是逗号分隔的枚举列表<?php$instr = &#39;测试&#39;;
// GBK转UTF-8$outstr = mb_convert_encoding($instr,&#39;UTF-8&#39;,&#39;GBK&#39;,);
$str = mb_convert_encoding($instr, "UCS-2LE", "JIS, eucjp-win, sjis-win");?>
Copy after login

##Personal suggestions when encountering transcoding problems It is safer to use mb_convert_encoding.

mb_convert_variables

mb_convert_variables —

Convert the character encoding of one or more variables

mb_convert_variables ( $to_encoding , $from_encoding , &$vars [, mixed &$... ] )
Copy after login

will The encoding of variable vars is converted from from_encoding to encoding to_encoding.

mb_convert_variables() 会拼接变量数组或对象中的字符串来检测编码,因为短字符串的检测往往会失败。因此,不能在一个数组或对象中混合使用编码。
Copy after login
to_encoding  将 string 转换成这个编码。

from_encoding 可以指定为一个 array 或者逗号分隔的 string,它将尝试根据 from-coding 来检测编码。 当省略了 from_encoding,将使用 detect_order。
vars 是要转换的变量的引用。 参数可以接受 String、Array 和 Object 的类型。 mb_convert_variables() 假设所有的参数都具有同样的编码。
额外的 vars。
Copy after login
返回值 :
成功时返回转换前的字符编码,失败时返回 FALSE。
Copy after login
实例:<?php/* 转换变量 $post1、$post2 编码为内部(internal)编码 
*/$interenc = mb_internal_encoding();$inputenc = mb_convert_variables($interenc,
 "ASCII,UTF-8,SJIS-win", $post1, $post2);?>
Copy after login

##mb_internal_encoding mb_internal_encoding — Set/get internal character encoding

mixed mb_internal_encoding ([ string $encoding = mb_internal_encoding() ] )
Copy after login
参数 :
encoding 字符编码名称使用于 HTTP 输入字符编码转换、HTTP 输出字符编码转换、mbstring 模块系列函数字符编码转换的默认编码。 
返回值 :
如果设置了 encoding,则成功时返回 TRUE, 或者在失败时返回 FALSE。 In this case, the character encoding for multibyte regex is NOT changed. 如果省略了 encoding,则返回当前的字符编码名称。
Copy after login
<?php/* 设置内部字符编码为 UTF-8 */mb_internal_encoding("UTF-8");/* 显示当前的内部字符编码*/echo mb_internal_encoding();?>
Copy after login

Detailed explanation of mb_detect_encoding: mb_detect_encoding —

Encoding of detected characters

string mb_detect_encoding ( string $str [, mixed $encoding_list = mb_detect_order() [, bool $strict = false ]] )
Copy after login
  • 1

检测字符串 str 的编码。

参数 
str    待检查的字符串。
encoding_list   是一个字符编码列表。 编码顺序可以由数组或者逗号分隔的列表字符串指定。
如果省略了 encoding_list 将会使用 detect_order。strict    strict 指定了是否严格地检测编码。 默认是 FALSE。
返回值
检测到的字符编码,或者无法检测指定字符串的编码时返回 FALSE。
Copy after login

字符串编码未知的情况下对字符串进行编码:
1、无论字符串编码是什么,均转换为gbk

function getSafeStr($str){
    $s1 = iconv(&#39;utf-8&#39;,&#39;gbk//IGNORE&#39;,$str);    $s0 = iconv(&#39;gbk&#39;,&#39;utf-8//IGNORE&#39;,$s1);    if($s0 == $str){        return $s1;
    }else{        return $str;
    }
}
Copy after login

2、无论字符串编码是什么,均转换为utf-8

function getSafeStr($str){
    $s1 = iconv(&#39;gbk&#39;,&#39;utf-8//IGNORE&#39;,$str);    $s0 = iconv(&#39;utf-8&#39;,&#39;gbk//IGNORE&#39;,$s1);    if($s0 == $str){        return $s1;
    }else{        return $str;
    }
}
Copy after login

获取字符串编码方法:

function getcode($str){
    $s1 = iconv(&#39;utf-8&#39;,&#39;gbk//IGNORE&#39;,$str);    $s0 = iconv(&#39;gbk&#39;,&#39;utf-8//IGNORE&#39;,$s1);    if($s0 == $str){        return &#39;utf-8&#39;;
    }else{        return &#39;gbk&#39;;
    }
}
Copy after login

The above is the detailed content of How to solve php encoding conversion garbled characters. For more information, please follow other related articles on the PHP Chinese website!

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1272
29
C# Tutorial
1252
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles