您的位置:首页 > 其它

关于mbstring的一些问题处理

2011-01-26 12:07 309 查看
一、关于mbstring多字节字符串处理模块的相关设置

今天正好在网上看到一篇关于如何设置mbstring系列函数为php默认使用函数的一篇文章
,顺便也就仔细看了看php.ini中mbstring部分的设置参数。mbstring系列函数在涉及到中文及其它亚洲字符集的开发中是经常使用的,研究一下还是有必要的。
先说文章中提及的一个参数:mbstring.func_overload。这个参数的好处
在于当你已经开发了大量程序后发现需要处理多字节字符集的时候,不可能将之前的程序全部检查,将相关函数替换成mbstring多字节字符处理函数。这个
时候你可以通过设置这个参数来使php默认使用mbstring系列函数来重载替代相对应的php内置函数(例如常用的substr()会被自动替换为
mb_substr())。有5个可选值:

0:代表不重载任何函数(默认值);

1:代表重载mail()函数;

2:代表重载str系列字符串处理函数;

4:代表重载ereg系列正则处理函数;

7:代表重载所有以上提及的函数。

不过在php手册中也提及了这么一句:

It is not recommended to use the function
overloading option in the per-directory context, because it’s not
confirmed yet to be stable enough in a production environment and may
lead to undefined behaviour.

哈哈,慎用慎用,毕竟这么强大无视的参数还是谨慎使用为好,影响太大。打开后影响的函数列表如下:

mail() -> mb_send_mail()strlen() ->
mb_strlen()strpos() -> mb_strpos()strrpos() ->
mb_strrpos()substr() -> mb_substr()strtolower() ->
mb_strtolower()strtoupper() -> mb_strtoupper()substr_count() ->
mb_substr_count()ereg() -> mb_ereg()eregi() ->
mb_eregi()ereg_replace() -> mb_ereg_replace()eregi_replace() ->
mb_eregi_replace()split() -> mb_split()

接下来几个参数也简略介绍一下,自己看注释理解的,可能有误:

mbstring.language:设置默认语言。默认值为neutral,就是UTF-8,这样就不错。

mbstring.internal_encoding:设置默认的内部编码。如果设置过mbstring.language,必须放置在之后,不然无法覆盖之前的设置。默认值NULL。

mbstring.http_input:默认的http输入编码。默认值pass,就是不处理。

mbstring.http_output:默认的http输出编码,前提是必须将output_buffering打开并将output_handler设置为mb_output_handler。默认值pass,也是不处理。

mbstring.encoding_translation:打开后将会自动将输入编码转换为internal_encoding所指定的编码(同样慎用,都交给机器未必是好事)。默认值Off,万幸。

mbstring.detect_order:设置编码的检测顺序,在使用mb_detect_encoding而又没有在第二个参数中指明检测
编码顺讯列表时,将会以这里设置的为准。默认值auto,在language为neutral时也就是代表检测顺序为(ASCII, UTF-8)。

mbstring.substitute_character:当编码无法转换时的处理方式。默认值为NULL,也就是不显示无法转换的字符。你也可以设置为long,显示字符的hex编码;或者指定其他特定字符(例如”@_@”,呵呵)。

mbstring.strict_detection:是否打开严格的编码检测模式,这个找了半天才知道什么意思,在处理有不同编码或错误编码字
符混杂情况(例如mb_detect_encoding(”testä”))下打开这个参数能够防止mb_detect_encoding返回错误的编
码。可以参见PHP Bug
24309
。默认值为Off。

mbstring.script_encoding:PHP脚本的默认编码。默认值为NULL。

大多数情况下默认值好像工作的就挺好,而且最好程序中也不要显式依赖于ini设置。使用mbstring系列函数时明确指明编码类型也许是一种更好的处理方式。在Unicode情况下,可以考虑设置为以下形式。

mbstring.language =
neutralmbstring.internal_encoding = UTF-8mbstring.encoding_translation =
Offmbstring.http_input = autombstring.http_output =
UTF-8mbstring.detect_order = autombstring.substitute_character = none

=================================
二、drupal的mbstrinmg:
[mbstring]
; language for internal character representation.
; http://php.net/mbstring.language mbstring.language = neutral
; internal/script encoding.
; Some encoding cannot work as internal encoding.
; (e.g. SJIS, BIG5, ISO-2022-*)
; http://php.net/mbstring.internal-encoding mbstring.internal_encoding = UTF-8
; http input encoding.
; http://php.net/mbstring.http-input mbstring.http_input = pass
; http output encoding. mb_output_handler must be
; registered as output buffer to function
; http://php.net/mbstring.http-output mbstring.http_output = pass
; enable automatic encoding translation according to
; mbstring.internal_encoding setting. Input chars are
; converted to internal encoding by setting this to On.
; Note: Do _not_ use automatic encoding translation for
;       portable libs/applications.
; http://php.net/mbstring.encoding-translation mbstring.encoding_translation = Off
; automatic encoding detection order.
; auto means
; http://php.net/mbstring.detect-order mbstring.detect_order = auto
; substitute_character used when character cannot be converted
; one from another
; http://php.net/mbstring.substitute-character ;mbstring.substitute_character = none;
; overload(replace) single byte functions by mbstring functions.
; mail(), ereg(), etc are overloaded by mb_send_mail(), mb_ereg(),
; etc. Possible values are 0,1,2,4 or combination of them.
; For example, 7 for overload everything.
; 0: No overload
; 1: Overload mail() function
; 2: Overload str*() functions
; 4: Overload ereg*() functions
; http://php.net/mbstring.func-overload ;mbstring.func_overload = 0
; enable strict encoding detection.
;mbstring.strict_detection = Off
; This directive specifies the regex pattern of content types for which mb_output_handler()
; is activated.
; Default: mbstring.http_output_conv_mimetype=^(text/|application/xhtml/+xml)
;mbstring.http_output_conv_mimetype=
; Allows to set script encoding. Only affects if PHP is compiled with --enable-zend-multibyte
; Default: ""
;mbstring.script_encoding=


注意

mbstring.http_output = pass
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: