Home  >  Article  >  Backend Development  >  How to convert character encoding to utf8 in php

How to convert character encoding to utf8 in php

hzc
hzcOriginal
2020-06-10 15:29:392141browse

How to convert character encoding to utf8 in php

php method to convert character encoding to utf8:

function strToUtf8($str){
    $encode = mb_detect_encoding($str, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5'));
    if($encode == 'UTF-8'){
        return $str;
    }else{
        return mb_convert_encoding($str, 'UTF-8', $encode);
    }
}

//mb_convert_encoding('String to be converted','Target encoding ','original encoding')
//mb_detect_encoding('string'):
mb_convert_encoding($str,'utf-8',mb_detect_encoding($str))

Recommended tutorial: " php tutorial

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