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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里