Home > Backend Development > PHP Tutorial > PHP study notes: Conversion and judgment of string encoding_PHP tutorial

PHP study notes: Conversion and judgment of string encoding_PHP tutorial

WBOY
Release: 2016-07-13 10:29:30
Original
759 people have browsed it

Copy code The code is as follows:

iconv('GBK', 'UTF-8//IGNORE', 'Script home'); // Convert the string from GBK encoding to UTF-8 encoding

But iconv can only solve the situation where the encoding is known in advance. If the string encoding is unknown, you need to detect its encoding first. In this case, the mb_string extension library may be used:

Copy code The code is as follows:

mb_detect_encoding('Script Home');

However, mb_detect_encoding has a flaw, and inaccurate judgment often occurs. Maybe this can solve it:

Copy code The code is as follows:

// Use iconv to convert and determine whether it is equivalent, which is not efficient
function is_utf8 ($str) {
if ($str === iconv('UTF-8', 'UTF-8//IGNORE', $str)) {
return 'UTF-8';
}
}
// Multiple encoding situations
function detect_encoding ($str) {
foreach (array('GBK', 'UTF-8') as $v) {
If ($str === iconv($v, $v . '//IGNORE', $str)) {
🎜>

After obtaining the string encoding information through the above method, you can use iconv or mb_convert_encoding to convert the encoding.


http://www.bkjia.com/PHPjc/774997.html

www.bkjia.com

truehttp: //www.bkjia.com/PHPjc/774997.htmlTechArticleCopy the code as follows: iconv('GBK', 'UTF-8//IGNORE', 'Script Home '); //Convert the string from GBK encoding to UTF-8 encoding but iconv can only solve the situation where the encoding is known in advance, if...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template