iconv_substr
(PHP 5, PHP 7, PHP 8)
iconv_substr — 截取字符串的部分
说明
string
$str
,int
$offset
,int
$length
= iconv_strlen($str, $charset),string
$charset
= ini_get("iconv.internal_encoding")): string
根据 offset
和 length
参数指定 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
-
如果省略了参数
charset
,string
的编码被认定为 iconv.internal_encoding。注意,
offset
和length
参数总是被认为字符表现的偏移,基于charset
检测到的字符集进行统计计算,而相对应的 substr() 则是基于字节的位移来计算。
返回值
返回 offset
和 length
参数指定的 str
的部分。
如果 str
比 offset
字符数更短,将会返回 false
。
如果 str
是 offset
个字符的长度,将返回空字符串。
更新日志
版本 | 说明 |
---|---|
7.0.11 |
如果 str 等长于
offset 个字符,
将返回空字符串。之前的版本里,这种情况是会返回 false 的。
|

User Contributed Notes 2 notes
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
<?=
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.
备份地址:http://www.lvesu.com/blog/php/function.iconv-substr.php