iconv编码类

属性
命名空间 fize\misc
类名 Iconv
常量:
名称 说明
WIN_UTF8_2_GBK 0 windows环境下的UTF8字符串转GBK
WIN_GBK_2_UTF8 1 windows环境下的GBK字符串转UTF8
方法:
方法名 说明
getEncoding() 获取 iconv 扩展的内部配置变量
mimeDecodeHeaders() 一次性解码多个 MIME 头字段
mimeDecode() 解码一个MIME头字段
mimeEncode() 编码一个MIME头
setEncoding() 为字符编码转换设定当前设置
strlen() 返回字符串的字符数统计
strpos() 查找字符串首次出现的位置
strrpos() 从右查找字符串首次出现的位置
substr() 截取字符串的部分
iconv() 将字符串 str 从 in_charset 转换编码到 out_charset。
serialize() 对要使用的字符串进行中文兼容性处理
winUtf8ToGbk() 方向 WIN_UTF8_2_GBK
winGbkToUtf8() 方向 WIN_GBK_2_UTF8

常量

WIN_UTF8_2_GBK

windows环境下的UTF8字符串转GBK

修饰符:public
类型:int
值:0

WIN_GBK_2_UTF8

windows环境下的GBK字符串转UTF8

修饰符:public
类型:int
值:1

方法

getEncoding()

获取 iconv 扩展的内部配置变量

public static function getEncoding (

    string $type = "all"

) : array|string
参数:
名称 说明
type 类型
返回值:

返回当前内部配置变量的值。

参数 `$type` :
可选值:all、input_encoding、output_encoding、internal_encoding

mimeDecodeHeaders()

一次性解码多个 MIME 头字段

public static function mimeDecodeHeaders (

    string $encoded_headers,

    int $mode = 0,

    string $charset = null

) : array
参数:
名称 说明
encoded_headers 编码过的头字符串。
mode 决定了遇到畸形 MIME 头字段时的行为
charset 指定编码
参数 `$mode` :
  可选值:ICONV_MIME_DECODE_STRICT、ICONV_MIME_DECODE_CONTINUE_ON_ERROR
参数 `$charset` :
  如果省略了,将使用 iconv.internal_encoding。

mimeDecode()

解码一个MIME头字段

public static function mimeDecode (

    string $encoded_header,

    int $mode = 0,

    string $charset = null

) : string
参数:
名称 说明
encoded_header 编码头,是一个字符串.
mode 决定了遇到畸形 MIME 头字段时的行为
charset 指定编码
返回值:

如果解码成功,返回一个被解码的MIME字段,

参数 `$mode` :
  可选值:ICONV_MIME_DECODE_STRICT、ICONV_MIME_DECODE_CONTINUE_ON_ERROR
参数 `$charset` :
  如果省略了,将使用 iconv.internal_encoding。
如果在解码过程中出现一个错误,将返回FALSE .

mimeEncode()

编码一个MIME头

public static function mimeEncode (

    string $field_name,

    string $field_value,

    array $preferences = null

) : string
参数:
名称 说明
field_name
field_value
preferences 可选参数
返回值:

返回编码后的字符串

setEncoding()

为字符编码转换设定当前设置

public static function setEncoding (

    string $type,

    string $charset

) : bool
参数:
名称 说明
type 类型
charset 字符集。
返回值:

成功时返回 TRUE, 或者在失败时返回 FALSE。

参数 `$type` :
可选值:input_encoding、output_encoding、internal_encoding

strlen()

返回字符串的字符数统计

public static function strlen (

    string $str,

    string $charset = null

) : int
参数:
名称 说明
str 该字符串
charset 编码
返回值:

返回 str 字符数的统计。

参数 `$charset` :
如果省略了 charset 参数,假设 str 的编码为 iconv.internal_encoding。

strpos()

查找字符串首次出现的位置

public static function strpos (

    string $haystack,

    string $needle,

    int $offset = 0,

    string $charset = null

) : int
参数:
名称 说明
haystack 要寻找的字符
needle 被查找的字符串
offset 偏移
charset 编码
参数 `$charset` :
如果省略了 charset 参数,假设 str 的编码为 iconv.internal_encoding。

strrpos()

从右查找字符串首次出现的位置

public static function strrpos (

    string $haystack,

    string $needle,

    string $charset = null

) : int
参数:
名称 说明
haystack 要寻找的字符
needle 被查找的字符串
charset 编码
参数 `$charset` :
如果省略了 charset 参数,假设 str 的编码为 iconv.internal_encoding。

substr()

截取字符串的部分

public static function substr (

    string $str,

    int $offset,

    int $length = null,

    string $charset = null

) : string
参数:
名称 说明
str 原始字符串。
offset 偏移
length 指定长度
charset 编码
返回值:

返回 offset 和 length 参数指定的 str 的部分。

参数 `$charset` :
  如果省略了 charset 参数,假设 str 的编码为 iconv.internal_encoding。
如果 str  offset 字符数更短,将会返回 FALSE 如果 str  offset 个字符的长度,将返回空字符串。

iconv()

将字符串 str 从 in_charset 转换编码到 out_charset。

public static function iconv (

    string $in_charset,

    string $out_charset,

    string $str

) : string
参数:
名称 说明
in_charset 输入的字符集。
out_charset 输出的字符集。
str 要转换的字符串。

serialize()

对要使用的字符串进行中文兼容性处理

public static function serialize (

    string $str,

    string $direction

) : string
参数:
名称 说明
str 待处理字符串
direction 方向 WIN_UTF8_2_GBK,WIN_GBK_2_UTF8
返回值:

处理后字符串

Windows、Linux系统针对中文字符创的兼容性处理
Windows由于使用GBK编码会导致中文路径乱码,进行UTF-8字符串转GBK字符串后再建立

winUtf8ToGbk()

方向 WIN_UTF8_2_GBK

public static function winUtf8ToGbk (

    string $str

) : string
参数:
名称 说明
str 待处理字符串

winGbkToUtf8()

方向 WIN_GBK_2_UTF8

public static function winGbkToUtf8 (

    string $str

) : string
参数:
名称 说明
str 待处理字符串