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条)

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?