How to convert php cp936 to utf8

藏色散人
Release: 2023-03-05 07:02:01
Original
5626 people have browsed it

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.

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

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

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

The result after the conversion was:

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

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);
    }
}
Copy after login

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!

Related labels:
php
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