dongyun8138 2015-05-25 13:35
浏览 111
已采纳

将数据分成基于两个或多个空格的数组元素

I'm trying to parse a tabbed text file which has on a single line multiple parts of data, the only way to differentiate between each part of data is that the parts are separated by gaps of two or more spaces or tabs.

I've found tons of answers on stack about replacing multiple spaces to a single space but haven't been able to get anything to answer my issue, I've tried playing around with the regex patterns to no avail.

 DER V3,0,0,3323  Xkisjd                 2014 02 25 05:23    PGM / RUN BY / DATE 

I was using something like this but it doesn't affect the data I would suspect this is due to the data begin separated by a tab.

 preg_split("/\s\s+/", $data, -1, PREG_SPLIT_NO_EMPTY);

I would appreciate any help I can get or suggestions.

Expected outcome would be an array:

 PGM => DER V3,0,0,3323
 RUN BY => Xkisjd
 DATE => 2014 02 25 05:23
  • 写回答

3条回答 默认 最新

  • douzhaishan5462 2015-05-25 13:48
    关注

    You need to modify your preg_split a bit. Check this out:-

    <?php
     $string ='DER V3,0,0,3323  Xkisjd                 2014 02 25 05:23    PGM / RUN BY / DATE'; 
    
    echo "<pre/>";print_r(preg_split("/\s\s+/",$string));
    
     ?>
    

    Output:- http://prntscr.com/796wd7

    If you want space will replace with - then do like this:-

    $data = preg_replace("/\s\s+/", "-", $string);
    echo $data;
    

    Output:- http://prntscr.com/7972nf

    Note:- It gives you an array. And i hope you know how to manipulate it according to your wish. thanks.

    I edited my answer and add second one because your question is bit confusing.I hope it will help you thanks.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line