Home  >  Article  >  Backend Development  >  What is the php gb2312 to utf8 function?

What is the php gb2312 to utf8 function?

藏色散人
藏色散人Original
2020-08-15 10:57:352279browse

php The gb2312 to utf8 function is "mb_convert_encoding", and its function syntax is "mb_convert_encoding($output['wd'], 'utf-8', 'gb2312');".

What is the php gb2312 to utf8 function?

Recommended: "PHP Video Tutorial"

PHP Convert UTF-8 and GB2312 URL encoding

In current WEB applications, UTF-8 encoding and GB2312 encoding coexist. For example, the URL encodings of Baidu (baidu.com) and Google (google.com) are GB2312 encoding and UTF respectively. -8 encoding. The garbled code problem caused by the coexistence of encodings brings a lot of trouble to WEB application development, so unified encoding is the main way to solve this problem in WEB development. The problem now is that we have to deal with UTF-8 encoding and GB2312 at the same time. For example, we want to count the keywords visited by a website from a search engine.

For example, we search for the word "Chinese" and then observe the browser address bar.

The result obtained using Baidu is:

http://www.baidu.com/s?wd=%D6%D0%CE%C4

The result obtained using Google is:

http://www.google.cn/search?hl=zh-CN&source=hp&q=%E4%B8%AD%E6%96%87&aq=f&oq=

The red characters are the URL encoding of the word "Chinese", that is, baidu_urlencode ("Chinese")=����, google_urlencode("Chinese")=Chinese, obviously different due to different encodings.

There is a mb_convert_encoding function in PHP, which brings convenience to the conversion of different encodings. The source code is as follows (the sample PHP source code document is archived in UTF-8 encoding):

header('Content-type: text/html; charset=utf-8');
echo '
百度关键词URL编码转换示例:
'; $urlBaidu = 'http://www.baidu.com/s?wd=%D6%D0%CE%C4'; $arrParse = parse_url($urlBaidu); parse_str($arrParse ['query'],$output); echo '乱码:' . $output['wd'] . '
'; //将gb2312编码转换成utf-8编码 $strUTF8 = mb_convert_encoding($output['wd'], 'utf-8', 'gb2312'); echo '可读码:' . $strUTF8 . '
'; echo '
谷歌关键词URL编码转换示例:
'; $urlGoogle = 'http://www.google.cn/search?hl=zh-CN&source=hp&q=%E4%B8%AD%E6%96%87&aq=f&oq='; $arrParse = parse_url($urlGoogle); parse_str($arrParse ['query'],$output); //谷歌关键词URL编码是utf-8,不用再作转换 echo '可读码:' . $output['q'] . '
';

The above is the detailed content of What is the php gb2312 to utf8 function?. For more information, please follow other related articles on the PHP Chinese website!

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