PHP detects current character encoding and transcoding

Source: Internet
Author: User

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 mb_detect_encoding - encoding of the detection character

Descriptionstringmb_detect_encoding( string $str[, mixed $encoding _list = Mb_detect_order () [, bool $strict = False ]] )

Detects the encoding of the string str.

Parameter 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 FALSEis returned if the encoding of the specified string cannot be detected.

2 mb_convert_encoding - encoding of converted characters

Descriptionstringmb_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.

Parameter str

The stringto 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.

PHP detects current character encoding and transcoding

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.