Php code conversion functions mb_convert_encoding and iconv

Source: Internet
Author: User
Php code conversion functions mb_convert_encoding and iconv

  1. Header ("content-type: text/html; charset = utf-8 ");
  2. Echo mb_convert_encoding (" my friends", "UTF-8", "gbk ");
  3. ?>

Encode and convert gb2312 to big5:

  1. Header ("content-type: text/html; charset = big5 ");
  2. Echo mb_convert_encoding ("You are my friend", "big5", "gb2312 ");
  3. ?>

To use the above php functions, you must install the enable mbstring Extension Library first.

The php function iconv is also used to convert string encoding, similar to the function above. Example: iconv-convert string to requested character encoding (php 4 >=4.0.5, php 5) mb_convert_encoding-convert character encoding (php 4 >=4.0.6, php 5)

Usage: string mb_convert_encoding (string str, string to_encoding [, mixed from_encoding]) first enable mbstring Extension Library, in php. ini will; extension = php_mbstring.dll before; remove mb_convert_encoding can specify multiple input encodings, it will automatically identify according to the content, but the execution efficiency is much worse than iconv;

String iconv (string in_charset, string out_charset, string str) Note: in addition to specifying the encoding to be converted, you can also add two suffixes: // transcoder and // ignore. // Transcoder automatically converts a character that cannot be directly converted into one or more similar characters, and // ignore ignores the characters that cannot be converted, by default, the result is truncated from the first invalid character. Returns the converted string or false on failure.

Usage: it is found that iconv will encounter an error when converting characters "-" to gb2312. without the ignore parameter, all strings after this character cannot be saved. In any case, the "-" cannot be converted successfully or output. In addition, mb_convert_encoding does not have this bug.

Generally, iconv is used. the mb_convert_encoding function is used only when the encoding of the original encoding cannot be determined or The iconv cannot be normally displayed after conversion.

From_encoding is specified by character code name before conversion. it can be array or string-comma separated enumerated list. if it is not specified, the internal encoding will be used. /* auto detect encoding from jis, eucjp-win, sjis-win, then convert str to ucs-2le */$ str = mb_convert_encoding ($ str, "ucs-2le", "jis, eucjp-win, sjis-win);/* "auto" is expanded to "ascii, jis, UTF-8, euc-jp, sjis "*/$ str = mb_convert_encoding ($ str," euc-jp "," auto ");

Example:

  1. $ Content = iconv ("gbk", "UTF-8", $ content );
  2. $ Content = mb_convert_encoding ($ content, "UTF-8", "gbk ");

The small trap of using mb_convert_encoding for transcoding in php code is familiar with the use of the mb_convert_encoding () method in the php program and is also widely used. In general, this method is also good enough and worthy of praise. However, in a project, we need to use it to convert utf8 to gbk, which is not a small problem when converting some special characters.

The specific manifestation is that mb converts UTF-8 encoded characters and non-encoded characters in gbk to \ 0x00 \ 0x80, in this way, the gbk character after conversion is problematic.

In the process of character encoding conversion, if a character that cannot be expressed by the target encoding is encountered, what the transcoding program should do is discard this character, although part of the data is lost, but it does not cause the character sequence of transcoding to be unavailable. It is unclear why the above method is used instead of the discard method.

Temporary solution: filter the transcoded string sequence to filter out all \ x00 \ 80 characters, or filter the UTF-8 string before escaping, filter out all characters that can be expressed by ut8 and not represented by gbk. in terms of implementation difficulty, the first filtering method is easier.

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.