MbString类

属性
命名空间 fize\misc
类名 MbString
方法:
方法名 说明
checkEncoding() 检查指定的字节流在指定的编码里是否有效
chr() 获取特定字符
convertCase() 对字符串进行大小写转换,转换模式由 mode 指定。
convertEncoding() 转换字符的编码
convertKana() 将“假名”从另一个转换为另一个(“zen-kaku”、“han-kaku”等等)
convertVariables() 转换一个或多个变量的字符编码
decodeMimeheader() 解码 MIME 头字段中的字符串
decodeNumericentity() 根据 HTML 数字字符串解码成字符
detectEncoding() 检测字符的编码
detectOrder() 设置/获取 字符编码的检测顺序
encodeMimeheader() 为 MIME 头编码字符串
encodeNumericentity() 将字符编码为HTML数字字符串引用
encodingAliases() 获取已知编码类型的别名
eregMatch() 正则表达式匹配多字节字符串
eregReplaceCallback() 使用回调函数执行正则表达式搜索并使用多字节支持替换
eregReplace() 用多字节支持替换正则表达式
eregSearchGetpos() 返回下一个正则表达式匹配的起始点
eregSearchGetregs() 从最后一个多字节正则表达式匹配中检索结果
eregSearchInit() 为多字节正则表达式匹配设置字符串和正则表达式
eregSearchPos() 返回预定义多字节正则表达式的匹配部分的位置和长度
eregSearchRegs() 返回多字节正则表达式的匹配部分
eregSearchSetpos() 设置下一个正则表达式匹配的起始点
eregSearch() 多字节正则表达式匹配预定义的多字节字符串
ereg() 正则表达式匹配多字节支持
eregiReplace() 用多字节支持忽略大小写替换正则表达式
eregi() 正则表达式匹配多字节支持的忽略大小写
getInfo() 获取 mbstring 的内部设置
httpInput() 检测 HTTP 输入字符的编码
httpOutput() 设置/获取 HTTP 输出字符编码
internalEncoding() 设置/获取内部字符编码
language() 设置/获取当前的语言
listEncodings() 返回所有支持编码的数组
ord() 获取字符的代码点
outputHandler() 在输出缓冲中转换字符编码的回调函数
parseStr() 解析 GET/POST/COOKIE 数据并设置全局变量
preferredMimeName() 获取 MIME 字符串
regexEncoding() 设置/获取多字节正则表达式的字符编码
regexSetOptions() 设置/获取mbregex函数的默认选项
scrub() 清理格式不正确的字符
sendMail() 发送编码过的邮件
split() 使用正则表达式分割多字节字符串
strcut() 获取字符的一部分
strimwidth() 获取按指定宽度截断的字符串
stripos() 大小写不敏感地查找字符串在另一个字符串中首次出现的位置
stristr() 大小写不敏感地查找字符串在另一个字符串里的首次出现
strlen() 获取字符串的长度
strpos() 查找字符串在另一个字符串中首次出现的位置
strrchr() 查找指定字符在另一个字符串中最后一次的出现
strrichr() 大小写不敏感地查找指定字符在另一个字符串中最后一次的出现
strripos() 大小写不敏感地在字符串中查找一个字符串最后出现的位置
strrpos() 查找字符串在一个字符串中最后出现的位置
strstr() 查找字符串在另一个字符串里的首次出现,并返回指定部分字符串
strtolower() 使字符串小写
strtoupper() 使字符串大写
strwidth() 返回字符串的宽度,多字节字符通常是单字节字符的两倍宽度。
substituteCharacter() 设置/获取替代字符
substrCount() 统计字符串出现的次数
substr() 获取部分字符串

方法

checkEncoding()

检查指定的字节流在指定的编码里是否有效

public static function checkEncoding (

    string $var = null,

    string $encoding = null

) : bool
参数:
名称 说明
var 要检查的字节流
encoding 期望的编码
该方法能有效避免所谓的"无效编码攻击(Invalid Encoding Attack)"
参数 `$var` :
  如果省略了参数 `$var` ,此函数会检查所有来自最初请求所有的输入。

chr()

获取特定字符

public static function chr (

    int $cp,

    string $encoding

) : string
参数:
名称 说明
cp 字符编码
encoding 编码

convertCase()

对字符串进行大小写转换,转换模式由 mode 指定。

public static function convertCase (

    string $str,

    int $mode,

    string $encoding = null

) : string
参数:
名称 说明
str 要转化的字符串
mode 转换的模式
encoding 字符编码
参数 `$mode` :
  可以是 MB_CASE_UPPER、 MB_CASE_LOWER  MB_CASE_TITLE 的其中一个
参数 `$encoding` :
  如果省略,则使用内部字符编码。

convertEncoding()

转换字符的编码

public static function convertEncoding (

    string $str,

    string $to_encoding,

    string $from_encoding = null

) : string
参数:
名称 说明
str 要转码的字符串
to_encoding 要转换成的编码类型。
from_encoding 原编码
参数 `$from_encoding` :
如果没有提供,则会使用内部(internal)编码。

convertKana()

将“假名”从另一个转换为另一个(“zen-kaku”、“han-kaku”等等)

public static function convertKana (

    string $str,

    string $option = "KV",

    string $encoding = null

) : string
参数:
名称 说明
str 要转码的字符串
option 转码选项
encoding 原编码
参数 `$encoding` :
如果没有提供,则会使用内部(internal)编码。

convertVariables()

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

public static function convertVariables (

    string $to_encoding,

    string $from_encoding,

    mixed &...$vars

) : string
参数:
名称 说明
to_encoding 指定编码
from_encoding 原编码
vars 要转换的变量的引用。
返回值:

转换前的字符编码

该方法假设所有的参数都具有同样的编码。
参数 `$vars` :
  可以接受 String、Array  Object 的类型。

decodeMimeheader()

解码 MIME 头字段中的字符串

public static function decodeMimeheader (

    string $str

) : string
参数:
名称 说明
str 要解码的字符串

decodeNumericentity()

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

public static function decodeNumericentity (

    string $str,

    array $convmap,

    string $encoding = null

) : string
参数:
名称 说明
str 要解码的字符串
convmap 指定了要转换的代码区域。
encoding 字符编码。
参数 `$encoding` :
如果省略参数 `$encoding` ,则使用内部字符编码。

detectEncoding()

检测字符的编码

public static function detectEncoding (

    string $str,

    array|string $encoding_list = null,

    bool $strict = false

) : string
参数:
名称 说明
str 待检查的字符串
encoding_list 字符编码列表
strict 是否严格地检测编码
编码顺序可以由数组或者逗号分隔的列表字符串指定

detectOrder()

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

public static function detectOrder (

    array|string $encoding_list = null

) : bool|array
参数:
名称 说明
encoding_list 一个数组或者逗号分隔的字符编码列表
设置编码检测顺序时候,成功时返回 TRUE,识别时候返回 FALSE
在获取编码检测顺序的时候,会返回排序过的编码数组。

encodeMimeheader()

为 MIME 头编码字符串

public static function encodeMimeheader (

    string $str,

    string $charset = null,

    string $transfer_encoding = "B",

    string $linefeed = "

",

    int $indent = 0

) : string
参数:
名称 说明
str 要编码的字符串
charset 指定了 str 的字符集名
transfer_encoding 指定了 MIME 的编码方案
linefeed 指定了 EOL(行尾)标记
indent 首行缩进(header 里 str 前的字符数目)
返回值:

转换后的字符串版本以 ASCII 形式表达。

参数 `$transfer_encoding` :
可以是 "B"(Base64)也可以是 "Q"(Quoted-Printable)。如果未设置,将回退为 "B"

encodeNumericentity()

将字符编码为HTML数字字符串引用

public static function encodeNumericentity (

    string $str,

    array $convmap,

    string $encoding = null,

    bool $is_hex = false

) : string
参数:
名称 说明
str 要编码的字符串
convmap 指定要转换的代码区域。
encoding 指定编码
is_hex 是否为16进制字符串

encodingAliases()

获取已知编码类型的别名

public static function encodingAliases (

    string $encoding

) : string[]
参数:
名称 说明
encoding 编码

eregMatch()

正则表达式匹配多字节字符串

public static function eregMatch (

    string $pattern,

    string $string,

    string $option = null

) : bool
参数:
名称 说明
pattern 正则表达式
string 要匹配的字符串
option 匹配的选项

eregReplaceCallback()

使用回调函数执行正则表达式搜索并使用多字节支持替换

public static function eregReplaceCallback (

    string $pattern,

    callable $callback,

    string $string,

    string $option = "msr"

) : string
参数:
名称 说明
pattern 正则表达式
callback 回调函数
string 要匹配的字符串
option 匹配的选项

eregReplace()

用多字节支持替换正则表达式

public static function eregReplace (

    string $pattern,

    string $replacement,

    string $string,

    string $option = "msr"

) : string
参数:
名称 说明
pattern 正则表达式
replacement 要替换成的字符串
string 要匹配的字符串
option 匹配的选项

eregSearchGetpos()

返回下一个正则表达式匹配的起始点

public static function eregSearchGetpos () : int

eregSearchGetregs()

从最后一个多字节正则表达式匹配中检索结果

public static function eregSearchGetregs () : string[]

eregSearchInit()

为多字节正则表达式匹配设置字符串和正则表达式

public static function eregSearchInit (

    string $string,

    string $pattern = null,

    string $option = null

) : bool
参数:
名称 说明
string 要匹配的字符串
pattern 正则表达式
option 匹配的选项

eregSearchPos()

返回预定义多字节正则表达式的匹配部分的位置和长度

public static function eregSearchPos (

    string $pattern = null,

    string $option = null

) : int[]
参数:
名称 说明
pattern 正则表达式
option 匹配的选项

eregSearchRegs()

返回多字节正则表达式的匹配部分

public static function eregSearchRegs (

    string $pattern = null,

    string $option = null

) : string[]
参数:
名称 说明
pattern 正则表达式
option 匹配的选项

eregSearchSetpos()

设置下一个正则表达式匹配的起始点

public static function eregSearchSetpos (

    int $position

) : bool
参数:
名称 说明
position 起始点

eregSearch()

多字节正则表达式匹配预定义的多字节字符串

public static function eregSearch (

    string $pattern = null,

    string $option = null

) : bool
参数:
名称 说明
pattern 正则表达式
option 匹配的选项

ereg()

正则表达式匹配多字节支持

public static function ereg (

    string $pattern,

    string $string,

    array &$regs = null

) : int
参数:
名称 说明
pattern 正则表达式
string 要匹配的字符串
regs 匹配项将存储在该数组的元素中

eregiReplace()

用多字节支持忽略大小写替换正则表达式

public static function eregiReplace (

    string $pattern,

    string $replace,

    string $string,

    string $option = "msr"

) : string
参数:
名称 说明
pattern 正则表达式
replace 要替换成的字符串
string 要匹配的字符串
option 匹配的选项

eregi()

正则表达式匹配多字节支持的忽略大小写

public static function eregi (

    string $pattern,

    string $string,

    array &$regs = null

) : int
参数:
名称 说明
pattern 正则表达式
string 要搜索的字符串
regs 匹配项将存储在该数组的元素中

getInfo()

获取 mbstring 的内部设置

public static function getInfo (

    string $type = null

) : mixed
参数:
名称 说明
type 指定返回类型
返回值:

如果没有指定 type 将返回类型信息的数组,否则将返回指定 type 的信息。

httpInput()

检测 HTTP 输入字符的编码

public static function httpInput (

    string $type = null

) : mixed
参数:
名称 说明
type 设置的字符串指定了输入类型
返回值:

如果没有处理过任何指定的 HTTP 输入,它将返回 FALSE。

参数 `$type` :
"G"  GET,"P"  POST,"C"  COOKIE,"S"  string,"L"  list,以及 "I" 是整个列表

httpOutput()

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

public static function httpOutput (

    string $encoding = null

) : mixed
参数:
名称 说明
encoding 设置编码
返回值:

如果省略了 encoding,返回当前的 HTTP 输出字符编码。否则成功时返回 TRUE, 或者在失败时返回 FALSE。

internalEncoding()

设置/获取内部字符编码

public static function internalEncoding (

    string $encoding = null

) : mixed
参数:
名称 说明
encoding 设置编码
返回值:

如果省略了 encoding,返回当前的内部字符编码。否则成功时返回 TRUE, 或者在失败时返回 FALSE。

language()

设置/获取当前的语言

public static function language (

    string $language = null

) : mixed
参数:
名称 说明
language 用于编码邮件信息
返回值:

如果设置了 language,并且 language 是有效的,它将返回 TRUE,否则将返回 FALSE。

有效的语言有:"Japanese","ja","English","en"  "uni"(UTF-8)。

listEncodings()

返回所有支持编码的数组

public static function listEncodings () : array

ord()

获取字符的代码点

public static function ord (

    string $str,

    string $encoding

) : int
参数:
名称 说明
str 字符串
encoding 编码

outputHandler()

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

public static function outputHandler (

    string $contents,

    int $status

) : string
参数:
名称 说明
contents 输出缓冲的内容
status 输出缓冲的状态

parseStr()

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

public static function parseStr (

    string $encoded_string,

    array &$result = null

) : bool
参数:
名称 说明
encoded_string URL 编码过的数据
result 一个数组,包含解码过的、编码转换后的值。

preferredMimeName()

获取 MIME 字符串

public static function preferredMimeName (

    string $encoding

) : string
参数:
名称 说明
encoding 要检查的字符串

regexEncoding()

设置/获取多字节正则表达式的字符编码

public static function regexEncoding (

    string $encoding = null

) : bool|string
参数:
名称 说明
encoding 编码
返回值:

设置时返回true或false,获取时返回string

regexSetOptions()

设置/获取mbregex函数的默认选项

public static function regexSetOptions (

    string $options = null

) : string
参数:
名称 说明
options 选项

scrub()

清理格式不正确的字符

public static function scrub (

    string $str,

    string $encoding

) : string
参数:
名称 说明
str 字符串
encoding 编码

sendMail()

发送编码过的邮件

public static function sendMail (

    string $to,

    string $subject,

    string $message,

    string $additional_headers = null,

    string $additional_parameter = null

) : bool
参数:
名称 说明
to 被发送到该邮件地址
subject 邮件标题
message 邮件消息
additional_headers 要插入到电子邮件标题末尾的字符串。
additional_parameter MTA 命令行参数
可通过逗号分隔地址的 `$to` 来指定多个收件人。该参数不会被自动编码
参数 `$additional_parameter` :
  在使用 sendmail 时对设置正确的返回路径头很有帮助

split()

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

public static function split (

    string $pattern,

    string $string,

    int $limit = null

) : array
参数:
名称 说明
pattern 正则表达式
string 待分割的字符串
limit 最多分割为 limit 个元素

strcut()

获取字符的一部分

public static function strcut (

    string $str,

    int $start,

    int $length = null,

    string $encoding = null

) : string
参数:
名称 说明
str 字符串
start 开始偏移量
length 长度
encoding 编码
参数 `$start` :
负表示返回的字符串是从 str 末尾处第 start 个字节开始的

strimwidth()

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

public static function strimwidth (

    string $str,

    int $start,

    int $width,

    string $trimmarker = null,

    string $encoding = null

) : string
参数:
名称 说明
str 字符串
start 开始位置的偏移
width 所需修剪的宽度。负数的宽度是从字符串结尾处统计的。
trimmarker 当字符串被截短的时候,将此字符串添加到截短后的末尾。
encoding 字符编码。如果省略,则使用内部字符编码
参数 `$start` :
从这些字符数开始的截取字符串。(默认是 0 个字符)。
如果 start 是负数,就是字符串结尾处的字符数。

stripos()

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

public static function stripos (

    string $haystack,

    string $needle,

    int $offset = 0,

    string $encoding = null

) : int|bool
参数:
名称 说明
haystack 被查找的字符串
needle 要查找这个字符串
offset 开始搜索的位置
encoding 使用的字符编码名称
返回值:

没找到返回false

参数 `$offset` :
  如果是负数,就从字符串的尾部开始统计。
参数 `$encoding` :
  如果省略了它,将使用内部字符编码。

stristr()

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

public static function stristr (

    string $haystack,

    string $needle,

    bool $before_needle = false,

    string $encoding = null

) : string|bool
参数:
名称 说明
haystack 被查找的字符串
needle 要查找这个字符串
before_needle 决定这个函数返回 haystack 的哪一部分
encoding 使用的字符编码名称
返回值:

返回指定部分字符串,没找到时返回false

参数 `$encoding` :
如果省略了它,将使用内部字符编码。

strlen()

获取字符串的长度

public static function strlen (

    string $str,

    string $encoding = null

) : int|bool
参数:
名称 说明
str 字符串
encoding 编码
返回值:

如果给定的 encoding 无效则返回 FALSE

strpos()

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

public static function strpos (

    string $haystack,

    string $needle,

    int $offset = 0,

    string $encoding = null

) : int|bool
参数:
名称 说明
haystack 被查找的字符串
needle 要查找这个字符串
offset 搜索位置的偏移
encoding 字符编码
返回值:

没找到时返回false

参数 `$offset` :
  如果没有提供该参数,将会使用 0。负数的 offset 会从字符串尾部开始统计。
参数 `$encoding` :
  如果省略,则使用内部字符编码。

strrchr()

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

public static function strrchr (

    string $haystack,

    string $needle,

    bool $before_needle = false,

    string $encoding = null

) : string|bool
参数:
名称 说明
haystack 被查找的字符串
needle 要查找这个字符串
before_needle 决定这个函数返回 haystack 的哪一部分
encoding 使用的字符编码名称
返回值:

返回指定部分字符串,没找到时返回false

参数 `$encoding` :
如果省略,则使用内部字符编码。

strrichr()

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

public static function strrichr (

    string $haystack,

    string $needle,

    bool $before_needle = false,

    string $encoding = null

) : string|bool
参数:
名称 说明
haystack 被查找的字符串
needle 要查找这个字符串
before_needle 决定这个函数返回 haystack 的哪一部分
encoding 使用的字符编码名称
返回值:

返回指定部分字符串,没找到时返回false

参数 `$encoding` :
如果省略,则使用内部字符编码。

strripos()

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

public static function strripos (

    string $haystack,

    string $needle,

    int $offset = 0,

    string $encoding = null

) : int|bool
参数:
名称 说明
haystack 被查找的字符串
needle 要查找这个字符串
offset 开始搜索的位置
encoding 使用的字符编码名称
返回值:

没找到时返回false

参数 `$encoding` :
如果省略,则使用内部字符编码。

strrpos()

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

public static function strrpos (

    string $haystack,

    string $needle,

    int $offset = 0,

    string $encoding = null

) : int|bool
参数:
名称 说明
haystack 被查找的字符串
needle 要查找这个字符串
offset 开始搜索的位置
encoding 使用的字符编码名称
返回值:

没找到时返回false

参数 `$encoding` :
如果省略,则使用内部字符编码。

strstr()

查找字符串在另一个字符串里的首次出现,并返回指定部分字符串

public static function strstr (

    string $haystack,

    string $needle,

    bool $before_needle = false,

    string $encoding = null

) : string|bool
参数:
名称 说明
haystack 被查找的字符串
needle 要查找这个字符串
before_needle 决定这个函数返回 haystack 的哪一部分
encoding 使用的字符编码名称
返回值:

没找到时返回false

参数 `$encoding` :
如果省略,则使用内部字符编码。

strtolower()

使字符串小写

public static function strtolower (

    string $str,

    string $encoding = null

) : string
参数:
名称 说明
str 字符串
encoding 使用的字符编码名称
参数 `$encoding` :
如果省略,则使用内部字符编码。

strtoupper()

使字符串大写

public static function strtoupper (

    string $str,

    string $encoding = null

) : string
参数:
名称 说明
str 字符串
encoding 使用的字符编码名称
参数 `$encoding` :
如果省略,则使用内部字符编码。

strwidth()

返回字符串的宽度,多字节字符通常是单字节字符的两倍宽度。

public static function strwidth (

    string $str,

    string $encoding = null

) : int
参数:
名称 说明
str 字符串
encoding 使用的字符编码名称
参数 `$encoding` :
如果省略,则使用内部字符编码。

substituteCharacter()

设置/获取替代字符

public static function substituteCharacter (

    mixed $substrchar = null

) : mixed
参数:
名称 说明
substrchar 指定替代选项
返回值:

设置时,在成功时返回 TRUE,失败时返回 FALSE。否则返回当前设置。

substrCount()

统计字符串出现的次数

public static function substrCount (

    string $haystack,

    string $needle,

    string $encoding = null

) : int
参数:
名称 说明
haystack 被查找的字符串
needle 要查找这个字符串
encoding 使用的字符编码名称
参数 `$encoding` :
如果省略,则使用内部字符编码。

substr()

获取部分字符串

public static function substr (

    string $str,

    int $start,

    int $length = null,

    string $encoding = null

) : string
参数:
名称 说明
str 字符串
start 开始下标
length 获取长度
encoding 使用的字符编码
参数 `$encoding` :
如果省略,则使用内部字符编码。