dongzhangnong2063 2014-06-28 22:20
浏览 123
已采纳

带有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-28 22: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) "/"
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部