How to solve php encoding conversion garbled characters
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’,'UTF-8′,$instr); ?>
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 = '测试'; // GBK转UTF-8$outstr = mb_convert_encoding($instr,'UTF-8','GBK',); $str = mb_convert_encoding($instr, "UCS-2LE", "JIS, eucjp-win, sjis-win");?>
mb_convert_variables
mb_convert_variables —Convert the character encoding of one or more variables
mb_convert_variables ( $to_encoding , $from_encoding , &$vars [, mixed &$... ] )
mb_convert_variables() 会拼接变量数组或对象中的字符串来检测编码,因为短字符串的检测往往会失败。因此,不能在一个数组或对象中混合使用编码。
to_encoding 将 string 转换成这个编码。 from_encoding 可以指定为一个 array 或者逗号分隔的 string,它将尝试根据 from-coding 来检测编码。 当省略了 from_encoding,将使用 detect_order。 vars 是要转换的变量的引用。 参数可以接受 String、Array 和 Object 的类型。 mb_convert_variables() 假设所有的参数都具有同样的编码。 额外的 vars。
返回值 : 成功时返回转换前的字符编码,失败时返回 FALSE。
实例:<?php/* 转换变量 $post1、$post2 编码为内部(internal)编码 */$interenc = mb_internal_encoding();$inputenc = mb_convert_variables($interenc, "ASCII,UTF-8,SJIS-win", $post1, $post2);?>
##mb_internal_encoding mb_internal_encoding — Set/get internal character encodingmixed mb_internal_encoding ([ string $encoding = mb_internal_encoding() ] )
参数 :
encoding 字符编码名称使用于 HTTP 输入字符编码转换、HTTP 输出字符编码转换、mbstring 模块系列函数字符编码转换的默认编码。
返回值 :
如果设置了 encoding,则成功时返回 TRUE, 或者在失败时返回 FALSE。 In this case, the character encoding for multibyte regex is NOT changed. 如果省略了 encoding,则返回当前的字符编码名称。
<?php/* 设置内部字符编码为 UTF-8 */mb_internal_encoding("UTF-8");/* 显示当前的内部字符编码*/echo mb_internal_encoding();?>
Detailed explanation of mb_detect_encoding: mb_detect_encoding —
Encoding of detected characters1 检测字符串 str 的编码。 字符串编码未知的情况下对字符串进行编码: 2、无论字符串编码是什么,均转换为utf-8 获取字符串编码方法: 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!string mb_detect_encoding ( string $str [, mixed $encoding_list = mb_detect_order() [, bool $strict = false ]] )
参数
str 待检查的字符串。
encoding_list 是一个字符编码列表。 编码顺序可以由数组或者逗号分隔的列表字符串指定。
如果省略了 encoding_list 将会使用 detect_order。strict strict 指定了是否严格地检测编码。 默认是 FALSE。
返回值
检测到的字符编码,或者无法检测指定字符串的编码时返回 FALSE。
1、无论字符串编码是什么,均转换为gbkfunction getSafeStr($str){
$s1 = iconv('utf-8','gbk//IGNORE',$str); $s0 = iconv('gbk','utf-8//IGNORE',$s1); if($s0 == $str){ return $s1;
}else{ return $str;
}
}
function getSafeStr($str){
$s1 = iconv('gbk','utf-8//IGNORE',$str); $s0 = iconv('utf-8','gbk//IGNORE',$s1); if($s0 == $str){ return $s1;
}else{ return $str;
}
}
function getcode($str){
$s1 = iconv('utf-8','gbk//IGNORE',$str); $s0 = iconv('gbk','utf-8//IGNORE',$s1); if($s0 == $str){ return 'utf-8';
}else{ return 'gbk';
}
}

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

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

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

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