PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

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

WBOY
WBOY 转载
2023-09-05 20:10:01 1171浏览

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()函数截取字符串的一部分?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除