
在 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。
立即学习“PHP免费学习笔记(深入)”;
iconv_substr()函数返回由偏移量和长度参数指定的字符串部分。如果字符串比偏移字符短,则返回 False。如果字符串与偏移字符的长度完全相同,则将返回 null 或空字符串。
<?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"
<?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()函数截取字符串的一部分?的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号