Home >Backend Development >PHP Problem >What should I do if the error message when writing a socket in PHP is garbled?

What should I do if the error message when writing a socket in PHP is garbled?

coldplay.xixi
coldplay.xixiOriginal
2020-07-11 11:21:222778browse

Solution to garbled error message when writing socket in PHP: First check the error message encoding, the code is [mb_detect_encoding(socket_last_error($socket)]; then convert the error message to [UTF-8] encoding; finally The conversion output result is normal.

What should I do if the error message when writing a socket in PHP is garbled?

Solution to garbled error message when writing socket in PHP:

1. First , use the following code to check the encoding of the error message

mb_detect_encoding(socket_last_error($socket))

The result is output as ASCII.

2. From the check result in the first step, we can know that the encoding of the error message is not UTF-8, so it needs Convert the error message to UTF-8 encoding. This can be achieved through the following function:

function doEncoding($str){
        $encode = strtoupper(mb_detect_encoding($str, ["ASCII",'UTF-8',"GB2312","GBK",'BIG5']));
        if($encode!='UTF-8'){
            $str = mb_convert_encoding($str, 'UTF-8', $encode);
        }
        return $str;
    }

After the code conversion, the output result is normal

Related learning recommendations:PHP programming from Beginner to master

The above is the detailed content of What should I do if the error message when writing a socket in PHP is garbled?. 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