PHP Detect Encoding and Change to UTF-8

Detect and convert encoding to UTF-8 in PHP.

1
2
3
4
5
6
7
8
9
function changeEncoding($text){
$encodeType = mb_detect_encoding($text, array('UTF-8', 'ASCII', 'GBK'));
if ($encodeType=='UTF-8') {
return $text; //No need to change
} else {
//return iconv($encodeType, "UTF-8//ignore", $text);
return mb_convert_encoding($text, "UTF-8", $encodeType); //Change to UTF-8
}
}