PHP explode() 函数把数组格式字符串或者字符串打散为数组

定义和用法

explode() 函数把字符串打散为数组。

注释:"separator" 参数不能是空字符串。

注释:该函数是二进制安全的。

语法

explode(separator,string,limit)
参数 描述
separator 必需。规定在哪里分割字符串。
string 必需。要分割的字符串。
limit 可选。规定所返回的数组元素的数目。

可能的值:

  • 大于 0 - 返回包含最多 limit 个元素的数组
  • 小于 0 - 返回包含除了最后的 -limit 个元素以外的所有元素的数组
  • 0 - 返回包含一个元素的数组

 

<?php
$str = 'one,two,three,four';

print_r(explode(',',$str));
// 零 limit
print_r(explode(',',$str,0));

// 正的 limit
print_r(explode(',',$str,2));

// 负的 limit
print_r(explode(',',$str,-1));
?>

结果

Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
)
Array
(
    [0] => one,two,three,four
)
Array
(
    [0] => one
    [1] => two,three,four
)
Array
(
    [0] => one
    [1] => two
    [2] => three
)

文章末尾固定信息

 
西部世界
  • 本文由 西部世界 发表于2021年2月4日 22:42:35
  • 转载请务必保留本文链接:https://www.cnhawkit.com/1441.html
  • explode() 函数
  • 把数组格式字符串或者字符串打散为数组
匿名

发表评论

匿名网友

拖动滑块以完成验证