iconv_substr

(PHP 5, PHP 7)

iconv_substr截取字符串的部分

说明

iconv_substr ( string $str , int $offset [, int $length = iconv_strlen($str, $charset) [, string $charset = ini_get("iconv.internal_encoding") ]] ) : string

根据 offsetlength 参数指定 str 截取的部分。

参数

str

原始字符串。

offset

如果 offset 是非负数,iconv_substr()str 开头第 offset 个字符开始截出部分,从 0 开始计数。

如果 offset 是负数,iconv_substr()str 末尾向前 offset 个字符开始截取。

length

如果指定了 length 并且是正数,返回的值从 offset 截取部分,最多包含 length 个字符(取决于 string 的长度)。

如果传入了负数的 lengthiconv_substr() 将从第 offset 个字符到离末尾 length 个字符截出 str 的部分。 如果 offset 也是负数,则开始位置计算规则的解释见以上。

charset

如果省略了参数 charsetstring 的编码被认定为 iconv.internal_encoding

注意,offsetlength 参数总是被认为字符表现的偏移,基于 charset 检测到的字符集进行统计计算,而相对应的 substr() 则是基于字节的位移来计算。

返回值

返回 offsetlength 参数指定的 str 的部分。

如果 stroffset 字符数更短,将会返回 FALSE。 如果 stroffset 个字符的长度,将返回空字符串。

更新日志

版本 说明
7.0.11 如果 str 等长于 offset 个字符, 将返回空字符串。之前的版本里,这种情况是会返回 FALSE 的。

参见

User Contributed Notes

doru87 at gmail dot com 21-Jan-2010 05:29
just fyi, iconv_substr() unknown error (0) has been resolved in PHP 5.2

you can also try mb_substr() or just substr() when dealing with this issue. Unfortunately there is no way to fix it in versions prior to 5.2.

Regards,
Teodor Sandu
qbolec at terra dot es 25-May-2007 02:07
<?=
iconv_substr
("A",0,1);
?>
generates folowing error:
Notice: iconv_substr() [function.iconv-substr]: Unknown error (0) in [...]

while, <?= iconv_substr("AB",0,1) ?> and <?= iconv_substr("AB",0,2) ?>
work as expected.