PHP implementation to detect the current character encoding and transcoding method

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.