HTTP 输入和输出

HTTP 输入/输出字符编码转换同样也适用于二进制数据。 如果 HTTP 输入/输出用到了二进制数据,用户应当控制字符的编码转换。

Note:

如果 HTML 表单的 enctype 属性设置为 multipart/form-data,并且 php.ini 里的 mbstring.encoding_translation 设置为 On, POST 的变量以及上传文件的名称也将会被转换到内部字符编码。 不过,转换不会应用于查询(query)的键。

  • HTTP 输入

    在 PHP 脚本里无法控制 HTTP 输入字符的转换。 要禁用 HTTP 输入字符的转换,必须要在 php.ini 里设置。

    Example #1 在 php.ini 中禁用 HTTP 输入转换

    ;; 禁用 HTTP 输入转换
    mbstring.http_input = pass
    ;;禁用 HTTP 输入转换 
    mbstring.encoding_translation = Off

    当 PHP 以 Apache 模块运行。这些设置还可以通过 httpd.conf 内每个虚拟主机(Virtual Host)指令或每个目录下的 .htaccess 来覆盖(override)。 详情参见配置这一节,以及 Apache 手册。

  • HTTP 输出

    输出字符编码转换的使用有几种方式。 一种是使用 php.ini,另一种是使用 ob_start(),以 mb_output_handler() 作为 ob_start 的回调函数。

Example #2 php.ini 设置例子

;; 为所有 PHP 页面启用输出字符编码的转换

;; 启用输出缓冲
output_buffering    = On

;; 设置 mb_output_handler 来进行输出的转换
output_handler      = mb_output_handler

Example #3 脚本例子

<?php

// 仅为此页面启用输出字符编码的转换

// 设置 HTTP 输出字符编码为 SJIS
mb_http_output('SJIS');

// 开始缓冲并指定 "mb_output_handler" 为回调函数
ob_start('mb_output_handler');

?>

相关文章
php 多字节字符串 函数 get a specific characterphp 多字节字符串 函数 对字符串进行大小写转换php 多字节字符串 函数 转换字符的编码php 多字节字符串 函数 convert kana one from another zen kaku han kaku and more php 多字节字符串 函数 retrieve the result from the last multibyte regular expression matchphp 多字节字符串 函数 setup string and regular expression for a multibyte regular expression matchphp 多字节字符串 函数 returns position and length of a matched part of the multibyte regular expression for a predefined multibyte stringphp 多字节字符串 函数 returns the matched part of a multibyte regular expressionphp 多字节字符串 函数 检测 http 输入字符编码php 多字节字符串 函数 设置/获取 http 输出字符编码php 多字节字符串 函数 在输出缓冲中转换字符编码的回调函数php 多字节字符串 函数 解析 get/post/cookie 数据并设置全局变量php 多字节字符串 函数 获取 mime 字符串php 多字节字符串 函数 set/get character encoding for multibyte regexphp 多字节字符串 函数 大小写不敏感地在字符串中查找一个字符串最后出现的位置php 多字节字符串 函数 查找字符串在一个字符串中最后出现的位置php 多字节字符串 函数 查找字符串在另一个字符串里的首次出现php 多字节字符串 函数 使字符串小写php 字符串 函数 转换字符串第一个字节为 0 255 之间的值php 多字节字符串 http 输入和输出
关注编程学问公众号