php的explode你会用了吗?

    |     2016年5月20日   |   学习偶记   |     评论已关闭   |    2939

经常用explode()来将字符串转成数组,但是有一个参数limit却很少用。今天记录下来,加深一下记忆。

运行示例:

$str = ‘one,two,three,four’;

参数:limit=0 返回包含一个元素的数组

print_r(explode(“,”,$str,0)) 返回结果:

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

参数:正的 limit 大于 0 返回包含最多 limit 个元素的数组

print_r(explode(“,”,$str,2)) 返回结果:

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

参数:负的 limit 小于 0 返回包含除了最后的 -limit 个元素以外的所有元素的数组

print_r(explode(“,”,$str,-1)) 返回结果:

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

噢!评论已关闭。