Home >Backend Development >PHP Tutorial >PHP implements method to detect current character encoding and transcode

PHP implements method to detect current character encoding and transcode

小云云
小云云Original
2018-03-28 15:35:091864browse

This article mainly shares with you the method of detecting the current character encoding and transcoding in PHP, combining text and code, hoping to help everyone.

1. Detect the current string encoding and change the encoding to utf-8
1 Get the encoding of the current string
$encode = mb_detect_encoding($str, array("ASCII",'UTF -8',"GB2312","GBK",'BIG5'));
2 Change the character encoding to utf-8
$str_encode = mb_convert_encoding($str, 'UTF-8', $encode) ;
2. Detailed explanation of function
1 mb_detect_encoding - Detect character encoding
Description
string mb_detect_encoding ( string $str [, mixed $encoding_list = mb_detect_order() [, bool $strict = false ]] )
Detect the encoding of the string str.
Parameters
str
The string to be checked.
encoding_list
encoding_list is a character encoding list. The encoding order can be specified by an array or a comma-separated list of strings.
If encoding_list is omitted, detect_order will be used.
strict
strict specifies whether to strictly detect encoding. Default is FALSE.
Return value
The detected character encoding, or FALSE when the encoding of the specified string cannot be detected.
2 mb_convert_encoding — Convert character encoding
Description
string mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding = mb_internal_encoding() ] )
Convert the character encoding of string type str from available The selected from_encoding is converted to to_encoding.
Parameters
str
The string to be encoded.
to_encoding
str The encoding type to be converted to.
from_encoding
Specified by character code name before conversion. It can be an array or a comma-separated list of enumerations. If from_encoding is not provided, the internal encoding will be used.
See supported encodings.
Return value
Encoded string.

Related recommendations:

PHP detects the current character encoding and transcodes

The above is the detailed content of PHP implements method to detect current character encoding and transcode. 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