文字

iconv_substr

(PHP 5, PHP 7)

iconv_substr截取字符串的部分

说明

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

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

参数

str

原始字符串。

offset

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

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

length

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

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

charset

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

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

返回值

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

如果 stroffset 字符数更短,将会返回 FALSE

参见

  • substr() - 返回字符串的子串
  • mb_substr() - 获取字符串的部分
  • mb_strcut() - 获取字符的一部分

用户评论:

[#1] doru87 at gmail dot com [2010-01-21 05:29:20]

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

[#2] qbolec at terra dot es [2007-05-25 02:07:50]

<?php=
iconv_substr("A",0,1);
?>

generates folowing error:
Notice: iconv_substr() [function.iconv-substr]: Unknown error (0) in [...]

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

上一篇: 下一篇: