php多字节字符串

Nov 21, 2016 pm 05:58 PM
php php字符串

简介

虽然许多语言每个必要字符都能一对一映射到 8 比特(bit)的值,但也有好几种语言需要非常多的字符来书面通讯,以至于它们的编码范围不能仅仅包含在一个字节里(一个字节 Byte 由 8 比特 bit 构成。每一比特仅能包含两种不同的值: 1 或 0。所以,一字节仅能够表示 256 种不同的值,即 2 的八次方)。 开发多字节字符编码方案是为了在基于字节的常规编码系统中表达超过 256 个字符。

在你操作(trim、split、splice 等等)多字节编码的字符串的时候,由于在这种编码方案下,两个或多个连续字节可能只表达了一个字符,所以你需要使用专门的函数。 否则,当你将不能检测多字节字符串的函数应用到这个字符串的时候,它可能无法检测多字节字符的起始位置,并以乱码字符串结尾,基本丢失了它原来的意思。

mbstring 提供了针对多字节字符串的函数,能够帮你处理 PHP 中的多字节编码。 除此以外,mbstring 还能在可能的字符编码之间相互进行编码转换。 为了方便起见,mbstring 设计成了处理基于 Unicode 的编码,类似 UTF-8、UCS-2 及诸多单字节的编码。

mbstring 不是一个默认扩展。这意味着它默认没有被激活。 你必须在 configure 选项中显式激活该模块。

HTTP 输入和输出

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

自 PHP 4.3.3 起,如果 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 输入转换 (PHP 4.3.0 或更高版本)
mbstring.encoding_translation = Off
Copier après la connexion

当 PHP 以 Apache 模块运行。这些设置还可以通过 httpd.conf 内每个虚拟主机(Virtual Host)指令或每个目录下的 .htaccess来覆盖(override)。
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
Copier après la connexion

Example #3 脚本例子

<?php
    // 仅为此页面启用输出字符编码的转换
    // 设置 HTTP 输出字符编码为 SJIS
    mb_http_output('SJIS');
    // 开始缓冲并指定 "mb_output_handler" 为回调函数
    ob_start('mb_output_handler');
?>
Copier après la connexion

多字节字符串 函数

mb_check_encoding — 检查字符串在指定的编码里是否有效

mb_convert_case — 对字符串进行大小写转换

mb_convert_encoding — 转换字符的编码

mb_convert_kana — Convert "kana" one from another ("zen-kaku", "han-kaku" and more)

mb_convert_variables — 转换一个或多个变量的字符编码

mb_decode_mimeheader — 解码 MIME 头字段中的字符串

mb_decode_numericentity — 根据 HTML 数字字符串解码成字符

mb_detect_encoding — 检测字符的编码

mb_detect_order — 设置/获取 字符编码的检测顺序

mb_encode_mimeheader — 为 MIME 头编码字符串

mb_encode_numericentity — Encode character to HTML numeric string reference

mb_encoding_aliases — Get aliases of a known encoding type

mb_ereg_match — Regular expression match for multibyte string

mb_ereg_replace_callback — Perform a regular expresssion seach and replace with multibyte support using a callback

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 a multibyte regular expression match

mb_ereg_search_pos — Returns position and length of a matched part of the multibyte regular expression for a predefined multibyte string

mb_ereg_search_regs — Returns the matched part of a 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 — 获取 mbstring 的内部设置

mb_http_input — 检测 HTTP 输入字符编码

mb_http_output — 设置/获取 HTTP 输出字符编码

mb_internal_encoding — 设置/获取内部字符编码

mb_language — 设置/获取当前的语言

mb_list_encodings — 返回所有支持编码的数组

mb_output_handler — 在输出缓冲中转换字符编码的回调函数

mb_parse_str — 解析 GET/POST/COOKIE 数据并设置全局变量

mb_preferred_mime_name — 获取 MIME 字符串

mb_regex_encoding — Set/Get character encoding for multibyte regex

mb_regex_set_options — Set/Get the default options for mbregex functions

mb_send_mail — 发送编码过的邮件

mb_split — 使用正则表达式分割多字节字符串

mb_strcut — 获取字符的一部分

mb_strimwidth — 获取按指定宽度截断的字符串

mb_stripos — 大小写不敏感地查找字符串在另一个字符串中首次出现的位置

mb_stristr — 大小写不敏感地查找字符串在另一个字符串里的首次出现

mb_strlen — 获取字符串的长度

mb_strpos — 查找字符串在另一个字符串中首次出现的位置

mb_strrchr — 查找指定字符在另一个字符串中最后一次的出现

mb_strrichr — 大小写不敏感地查找指定字符在另一个字符串中最后一次的出现

mb_strripos — 大小写不敏感地在字符串中查找一个字符串最后出现的位置

mb_strrpos — 查找字符串在一个字符串中最后出现的位置

mb_strstr — 查找字符串在另一个字符串里的首次出现

mb_strtolower — 使字符串小写

mb_strtoupper — 使字符串大写

mb_strwidth — 返回字符串的宽度

mb_substitute_character — 设置/获取替代字符

mb_substr_count — 统计字符串出现的次数

mb_substr — 获取字符串的部分


Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD

Tags d'article chaud

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian Dec 24, 2024 pm 04:42 PM

Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian

Date et heure de CakePHP Date et heure de CakePHP Sep 10, 2024 pm 05:27 PM

Date et heure de CakePHP

Configuration du projet CakePHP Configuration du projet CakePHP Sep 10, 2024 pm 05:25 PM

Configuration du projet CakePHP

Téléchargement de fichiers CakePHP Téléchargement de fichiers CakePHP Sep 10, 2024 pm 05:27 PM

Téléchargement de fichiers CakePHP

Routage CakePHP Routage CakePHP Sep 10, 2024 pm 05:25 PM

Routage CakePHP

Discuter de CakePHP Discuter de CakePHP Sep 10, 2024 pm 05:28 PM

Discuter de CakePHP

Comment configurer Visual Studio Code (VS Code) pour le développement PHP Comment configurer Visual Studio Code (VS Code) pour le développement PHP Dec 20, 2024 am 11:31 AM

Comment configurer Visual Studio Code (VS Code) pour le développement PHP

Guide rapide CakePHP Guide rapide CakePHP Sep 10, 2024 pm 05:27 PM

Guide rapide CakePHP

See all articles