php函数mb_convert_encoding()使用详解

Multibyte String,专门处理多字节字符串的.
以下是php官方英文原文说明文档:
While there are many languages in which every necessary character can be represented by a one-to-one mapping to an 8-bit value, there are also several languages which require so many characters for written communication that they cannot be contained within the range a mere byte can code (A byte is made up of eight bits. Each bit can contain only two distinct values, one or zero. Because of this, a byte can only represent 256 unique values (two to the power of eight)). Multibyte character encoding schemes were developed to express more than 256 characters in the regular bytewise coding system.

When you manipulate (trim, split, splice, etc.) strings encoded in a multibyte encoding, you need to use special functions since two or more consecutive bytes may represent a single character in such encoding schemes. Otherwise, if you apply a non-multibyte-aware string function to the string, it probably fails to detect the beginning or ending of the multibyte character and ends up with a corrupted garbage string that most likely loses its original meaning.

mbstring provides multibyte specific string functions that help you deal with multibyte encodings in PHP. In addition to that, mbstring handles character encoding conversion between the possible encoding pairs. mbstring is designed to handle Unicode-based encodings such as UTF-8 and UCS-2 and many single-byte encodings for convenience (listed below).

mb_convert_encoding的用法见官方:
http://cn.php.net/manual/zh/function.mb-convert-encoding.php

做一个GBK To UTF-8

Php代码 复制代码
  • <?php
  • header(“content-Type: text/html; charset=Utf-8”);
  • echo mb_convert_encoding(“妳係我的友仔”, “UTF-8”, “GBK”);
  • ?>
  • <?php
    header("content-Type: text/html; charset=Utf-8");
    echo mb_convert_encoding("妳係我的友仔", "UTF-8", "GBK");
    ?>

    再来个GB2312 To Big5

    Php代码 复制代码
  • <?php
  • header(“content-Type: text/html; charset=big5”);
  • echo mb_convert_encoding(“你是我的朋友”, “big5”, “GB2312”);
  • ?>
  • <?php
    header("content-Type: text/html; charset=big5");
    echo mb_convert_encoding("你是我的朋友", "big5", "GB2312");
    ?>

    不过要使用上面的函数需要安装但是需要先enable mbstring 扩展库。

    PHP中的另外一个函数iconv也是用来转换字符串编码的,与上函数功能相似。

    下面还有一些详细的例子:
    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)

    用法:
    string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )
    需要先enable mbstring 扩展库,在 php.ini里将; extension=php_mbstring.dll 前面的 ; 去掉
    mb_convert_encoding 可以指定多种输入编码,它会根据内容自动识别,但是执行效率比iconv差太多;

    string iconv ( string in_charset, string out_charset, string str )
    注意:第二个参数,除了可以指定要转化到的编码以外,还可以增加两个后缀://TRANSLIT 和 //IGNORE,其中 //TRANSLIT 会自动将不能直接转化的字符变成一个或多个近似的字符,//IGNORE 会忽略掉不能转化的字符,而默认效果是从第一个非法字符截断。
    Returns the converted string or FALSE on failure.

    使用:

    发现iconv在转换字符”—”到gb2312时会出错,如果没有ignore参数,所有该字符后面的字符串都无法被保存。不管怎么样,这个”—”都无法转换成功,无法输出。另外mb_convert_encoding没有这个bug.

    一般情况下用 iconv,只有当遇到无法确定原编码是何种编码,或者iconv转化后无法正常显示时才用mb_convert_encoding 函数.

    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.

    $str = mb_convert_encoding($str, “UCS-2LE”, “JIS, eucjp-win, sjis-win”);

    $str = mb_convert_encoding($str, “EUC-JP”, “auto”);

    例子:
    $content = iconv(”GBK”, “UTF-8″, $content);
    $content = mb_convert_encoding($content, “UTF-8″, “GBK”);

    PHP中使用mb_convert_encoding转码的小陷阱
    在php程序中使用mb_convert_encoding()方法进行字符编码转换大家都很熟悉了,平时也在大量的使用。而且在一般情况下该方法也表现的足够好,值得表扬。但在一个项目中我们需要使用它进行UTF8到GBK的转换,在转换一些特殊字符时发现了一个不大不小的问题。具体表现为mb把在utf8可编码的字符而在gbk中不可编码的字符都转成了�x00�x80,这样就导致转换后的gbk字符是有问题的。
    在我们的意识中,在进行字符编码转换的过程中,如果遇到目标编码不可表现的字符,转码程序应该做的是舍弃这种字符,这样虽然丢失了部分数据,但不会导致转码的字符序列不可用。不清楚mb为什么要使用上述方式而不是舍弃方式。
    临时的解决方式是对转码后的字符串序列进行过滤,过滤掉所有x0080的字符;又或者在转义之前对utf8的字符串进行过滤,过滤掉ut8可表示而gbk不可表示的所有字符,从实现难度上来讲,第一种过滤方式比较容易做到。

    mb_convert_encoding()函数对于中文及其他语言是相对安全的,基本上,php的string类型函数,都可以找到对应的mb函数族函数,mb_函数族全部函数如下:
    目录
    mb_convert_case — Perform case folding on a string
    mb_convert_encoding — Convert character encoding
    mb_convert_kana —  Convert “kana” one from another (“zen-kaku”, “han-kaku” and more)
    mb_convert_variables — Convert character code in variable(s)
    mb_decode_mimeheader — Decode string in MIME header field
    mb_decode_numericentity —  Decode HTML numeric string reference to character
    mb_detect_encoding — Detect character encoding
    mb_detect_order —  Set/Get character encoding detection order
    mb_encode_mimeheader — Encode string for MIME header
    mb_encode_numericentity —  Encode character to HTML numeric string reference
    mb_ereg_match —  Regular expression match for multibyte string
    mb_ereg_replace — Replace regular expression with multibyte support
    mb_ereg_search_getpos —  Returns start point for next regular expression match
    mb_ereg_search_getregs —  Retrieve the result from the last multibyte regular expression match
    mb_ereg_search_init —  Setup string and regular expression for multibyte regular expression match
    mb_ereg_search_pos —  Return position and length of matched part of multibyte regular expression for predefined multibyte string
    mb_ereg_search_regs —  Returns the matched part of multibyte regular expression
    mb_ereg_search_setpos —  Set start point of next regular expression match
    mb_ereg_search —  Multibyte regular expression match for predefined multibyte string
    mb_ereg — Regular expression match with multibyte support
    mb_eregi_replace —  Replace regular expression with multibyte support ignoring case
    mb_eregi —  Regular expression match ignoring case with multibyte support
    mb_get_info — Get internal settings of mbstring
    mb_http_input — Detect HTTP input character encoding
    mb_http_output — Set/Get HTTP output character encoding
    mb_internal_encoding —  Set/Get internal character encoding
    mb_language —  Set/Get current language
    mb_list_encodings —  Returns an array of all supported encodings
    mb_output_handler —  Callback function converts character encoding in output buffer
    mb_parse_str —  Parse GET/POST/COOKIE data and set global variable
    mb_preferred_mime_name — Get MIME charset string
    mb_regex_encoding —  Returns current encoding for multibyte regex as string
    mb_regex_set_options —  Set/Get the default options for mbregex functions
    mb_send_mail —  Send encoded mail
    mb_split — Split multibyte string using regular expression
    mb_strcut — Get part of string
    mb_strimwidth — Get truncated string with specified width
    mb_strlen — Get string length
    mb_strpos —  Find position of first occurrence of string in a string
    mb_strrpos —  Find position of last occurrence of a string in a string
    mb_strtolower — Make a string lowercase
    mb_strtoupper — Make a string uppercase
    mb_strwidth — Return width of string
    mb_substitute_character — Set/Get substitution character
    mb_substr_count — Count the number of substring occurrences
    mb_substr — Get part of string

    相关文章

    发表评论

    返回顶部