douweng7308 2013-09-13 14:50
浏览 15
已采纳

如何将带有前导和尾随字符的分隔字符串拆分为数组?

For example, if I have this string:

$stuff = "[1379082600-1379082720],[1379082480-1379082480],[1379514420-1379515800],";

I know can do this to split it into an array like this:

$stuff = str_replace(array("[","]"),array("",""),$stuff);
$stuff = explode(",",$stuff);

But it seems like there would be an easier way since the string is already in an array form almost. Is there an easier way?

  • 写回答

5条回答 默认 最新

  • doulu7174 2013-09-13 15:17
    关注

    You can get inside [] with preg_match_all. Try following:

    preg_match_all("/\[(.*?)\]/",$stuff, $matches);
    

    Output of $matches[1]

    array (size=3)
      0 => string '1379082600-1379082720' (length=21)
      1 => string '1379082480-1379082480' (length=21)
      2 => string '1379514420-1379515800' (length=21)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?