Home > Backend Development > PHP Problem > Why are the parameters passed in php garbled?

Why are the parameters passed in php garbled?

藏色散人
Release: 2023-03-14 15:18:01
Original
2019 people have browsed it

The reason why php parameters are garbled is because the encoding is inconsistent. The solution is to modify it to use uft-8 encoding. The modification code is such as "mb_detect_encoding($typeName,'CP936,UTF-8')".

Why are the parameters passed in php garbled?

The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

Why are the parameters in php garbled?

Solution to garbled parameters in php

When the website project uses gb2312 as the project encoding, due to different browsers The encoding of Chinese characters may be different. Some will encode Chinese into website encoding, and some will encode Chinese into utf-8. Garbled errors will occur when receiving parameters in the background. A better solution is to identify the Chinese characters in the parameters and convert them if they are not gb2312.

$typeName = $_GET['typeName'];
$encode =  mb_detect_encoding($typeName,'CP936,UTF-8') ;
//如果是CP936,就是gbk编码,如果是UTF-8, 就是utf-8编码
if ($encode =='UTF-8') {
    //再调用转换成utf-8编码的函数,最终确保是utf-8
    $typeName = iconv("utf-8","gb2312",$typeName);
}
Copy after login

For website coding issues and future expansion, it is recommended to use uft-8 encoding

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Why are the parameters passed in php garbled?. For more information, please follow other related articles on the PHP Chinese website!

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