This article mainly share with you the implementation of PHP to detect the current character encoding and transcoding methods, combined with text and code, hope to help everyone.
First, detect the current string encoding and change the encoding to Utf-8
1 Gets the encoding of the current string
$encode = mb_detect_encoding ($str, Array ("ASCII", ' UTF-8 ', "GB2312", "GBK", ' BIG5 '));
2 change character encoding to Utf-8
$str _encode = mb_convert_encoding ($str, ' UTF-8 ', $encode);
Second, the function of the detailed
1 encoding of mb_detect_encoding-detection characters
Description
String mb_detect_encoding (String $str [, mixed $encoding _list = Mb_detect_order () [, bool $strict = false]])
Detects the encoding of the string str.
Parameters
Str
The string to be checked.
Encoding_list
Encoding_list is a list of character encodings. The encoding order can be specified by an array or comma-delimited list string.
If Encoding_list is omitted, the Detect_order will be used.
Strict
Strict specifies whether to strictly detect the encoding. The default is FALSE.
return value
The character encoding is detected, or FALSE is returned if the encoding of the specified string cannot be detected.
2 encoding of mb_convert_encoding-conversion characters
Description
String mb_convert_encoding (String $str, String $to _encoding [, mixed $from _encoding = mb_internal_encoding ()])
Converts the character encoding of the string type str from an optional from_encoding to a to_encoding.
Parameters
Str
The string to encode.
To_encoding
The type of encoding to which STR is to be converted.
From_encoding
specified by the character code name before conversion. It can be an array or a comma-delimited list of enumerations. If from_encoding is not provided, the internal (internal) encoding is used.
See supported encodings.
return value
The encoded string.
Related recommendations:
PHP detects current character encoding and transcoding