您好,欢迎访问一九零五行业门户网

PHP - 如何使用iconv_substr()函数截取字符串的一部分?

在 php 中,iconv_substr() 函数用于通过偏移量和长度参数来剪切指定字符串的一部分。假设我们有一个字符串“helloworld”,并且我们只想剪切并显示字符串(llowo),那么我们将使用2到5之间的数字来选择它。
语法string iconv_substr(str $string, int $offset, int $length, str $encoding)
参数iconv_substr()接受四个参数:$string、$offset、$length 和 $encoding。
$string− $string 参数指定原始编码
$offset− 如果 $offset 参数非负,则 iconv_substr() 函数会剪切从偏移字符开始的字符串的选定部分,从零开始计数。如果为负数,则 iconv_substr() 函数会剪切从该位置开始的部分,从字符串末尾偏移字符。
$length− 如果给定了 $length 参数且为正数,则其返回值最多包含从 offset 开始的部分的 length 个字符。
$encoding− 如果编码参数不存在或为 null,则假定该字符串采用 iconv.internal_encoding。
返回值iconv_substr()函数返回由偏移量和长度参数指定的字符串部分。如果字符串比偏移字符短,则返回 false。如果字符串与偏移字符的长度完全相同,则将返回 null 或空字符串。
示例 1无空格读取的 iconv_substr() 函数 现场演示<?php // helloworld sting is used // to cut the selected portion from string //iconv_substr function is used $string = iconv_substr("helloworld!", 2, 7, "utf-8"); // it will returns the character from 2 to 7 var_dump($string);?>
输出string(7) "lloworl"
示例 2带有空格读取的 iconv_substr() 函数 实时演示<?php // helloworld sting is used // to cut the selected portion from string //iconv_substr function is used $string = iconv_substr ("hello world!", 2, 7, "utf-8"); // it will returns the character from 2 to 7 var_dump($string);?gt;
输出string(7) "llo wor"
以上就是php - 如何使用iconv_substr()函数截取字符串的一部分?的详细内容。
其它类似信息

推荐信息