Home >Backend Development >PHP Problem >How to convert php cp936 to utf8

How to convert php cp936 to utf8

藏色散人
藏色散人Original
2020-08-24 10:07:155679browse

php cp936 to utf8 setting method: first create a php sample file; then define a toUtf8 method; then pass [iconv('utf-8', 'latin1//IGNORE', $str);] method to solve the conversion problem.

How to convert php cp936 to utf8

Recommended: "PHP Video Tutorial"

php cp936 to utf8 Chinese encoding conversion solution to the problem of garbled Chinese characters

I encountered a problem today.

中文字符串 如  åç°ä»£è½»å¥¢å
¨éåç

needed to be converted to UTF8, so I called the PHP function.

mb_convert_encoding($str, 'UTF-8', 'CP936');

The result after the conversion was:

氓聬聨莽聨掳盲禄拢猫陆禄氓楼垄氓聟篓茅聯聹氓聬聤莽聛炉

The actual required result is:

Postmodern light luxury all-copper chandelier

When I encountered this problem, I searched on Baidu and found that no solution was found

It’s best to try it yourself, solve it, and share the record with those who need it to prevent you from getting into trouble again!

The solution is as follows:

Use iconv('utf-8', 'latin1//IGNORE', $str);

Attachment:

function toUtf8($str) {
    $encode = mb_detect_encoding($str, array('CP936', "ASCII","GB2312","GBK",'UTF-8','BIG5'));
    if ($encode == 'UTF-8') {
        return $str;
    } elseif ($encode == 'CP936') {
        return iconv('utf-8', 'latin1//IGNORE', $str);
    } else {
        return mb_convert_encoding($str, 'UTF-8', $encode);
    }
}

The above is the detailed content of How to convert php cp936 to utf8. 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