PHP encoding conversion function mb_convert_encoding and iconv

WBOY
Release: 2016-07-25 08:53:51
Original
1102 people have browsed it
  1. header("content-type: text/html; charset=utf-8");
  2. echo mb_convert_encoding("You are my friend", "utf-8", "gbk ");
  3. ?>
Copy the code

encoding to convert gb2312 to big5:

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

Using the above php functions requires installation but you need to enable the mbstring extension library first.

The php function iconv is also used to convert string encoding, and its function is 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] ) You need to enable the mbstring extension library first, and remove the ; in front of extension=php_mbstring.dll in php.ini mb_convert_encoding can specify multiple input encodings, which will be automatically identified based on 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 to, the second parameter can also add two suffixes: //translit and //ignore, where //translit will automatically convert characters that cannot be directly converted into one or more Approximate characters, //ignore will ignore characters that cannot be converted, and the default effect is to truncate from the first illegal character. returns the converted string or false on failure.

Use: It was found that iconv would make an error when converting the character "-" to gb2312. Without the ignore parameter, all strings following this character cannot be saved. No matter what, this "-" cannot be converted successfully and cannot be output. In addition, mb_convert_encoding does not have this bug.

In general, use iconv. Only use the mb_convert_encoding function when you are unable to determine what the original encoding is, or when iconv cannot be displayed normally 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");
Copy code

Small pitfalls of using mb_convert_encoding in php encoding Everyone is familiar with using the mb_convert_encoding() method to convert character encodings in PHP programs, and it is also used in large quantities. And in general, this method performs well enough and deserves praise. But in a project we needed to use it to convert utf8 to gbk, and found a minor problem when converting some special characters.

The specific performance is that mb converts characters that can be encoded in utf8 but cannot be encoded in gbk.

During the character encoding conversion process, if a character that cannot be represented by the target encoding is encountered, what the transcoding program should do is discard such characters. In this way, although some data is lost, it will not cause the transcoded character sequence to be unusable. . It's not clear why mb should use the above method instead of discarding it.

Temporary solution: Filter the transcoded string sequence to filter out all x0080 characters; or filter the utf8 string before escaping to filter out all characters that can be represented by ut8 but cannot be represented by gbk. In terms of implementation difficulty , the first filtering method is easier to do.


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template