dongzhangnong2063 2014-06-29 06:20
浏览 120
已采纳

带有PREG_SPLIT_DELIM_CAPTURE标志的preg_split仍然会使用分隔符

String $uri:

/profile/asd/

I run this on the string:

$uri_arr = preg_split('/\//',$uri,-1,PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);

Result:

Array ( [0] => profile [1] => asd )

I thought PREG_SPLIT_DELIM_CAPTURE was supposed to conserve / if not I would use explode instead...

  • 写回答

1条回答 默认 最新

  • dow66098 2014-06-29 06:25
    关注

    To capture the delimiters, you still need to wrap them into parentheses:

    preg_split('~(/)~', '/profile/asd/', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY)
    

    Output:

    array(5) {
      [0]=>
      string(1) "/"
      [1]=>
      string(7) "profile"
      [2]=>
      string(1) "/"
      [3]=>
      string(3) "asd"
      [4]=>
      string(1) "/"
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler