douyan6958 2014-02-08 05:36
浏览 5
已采纳

PHP爆炸无法正常工作

This might seem trivial but it's hurting my head. Can someone explain the reason The http://php.net/manual/es/function.explode.php examples show (which I have trimmed).

$pizza  = "piece1 piece2 piece3"; // string
$pieces = explode(" ", $pizza);
// after a var dump
array(3) {  [0]=> string(6) "piece1" 
            [1]=> string(6) "piece2" 
            [2]=> string(6) "piece3" }

This works lovely, removes all the spaces and does a nice little array to work with, however when I use something similar shown below

$path = "/test-gallery/2/"; // string
$urlpieces = explode("/", $path);
// after a var dump
array(4) {  [0]=> string(0) "" 
            [1]=> string(12) "test-gallery" 
            [2]=> string(1) "2" 
            [3]=> string(0) "" }

I get the first and last with an empty string. Why does it not remove the first and the last array elements? I can always add another step and remove it but the explode should take it all out shouldnt it?

Thanks for the advice in advance.

  • 写回答

1条回答 默认 最新

  • douyo770657 2014-02-08 05:52
    关注

    I can always add another step and remove it but the explode should take it all out shouldnt it?

    No. that is how explode works. Delimiter is at both end so "" string was found at both the ends after exploding

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集