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

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

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.

If I understand your question correctly, you are expecting the string to be updated after executing preg_split?

Refer to the manual: preg_split will split by the delimiter, so it will return an array. You need preg_replace:

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

That will give you the desired result.

Another note: the subject of question says you want to replace space ( ) characters with a dash (-) but in your question you talk about replacing multiple spaces with a single space?

I suggest another approach with preg_match_all.

Regex:

#^([a-z]+\s+[a-z]\d+(?:,\d+)+)\s*(\S+)\s*([\d:\s]+\d)\s*([^\/]+)\s+\/\s+([^\/]+)\s+\/\s+([^\/]+)\s+$#im

Here is a sample code:

$re = "/^([a-z]+\\s+[a-z]\\d+(?:,\\d+)+)\\s*(\\S+)\\s*([\\d:\\s]+\\d)\\s*([^\\/]+)\\s+\\/\\s+([^\\/]+)\\s+\\/\\s+([^\\/]+)\\s+$/mi"; 
$str = "DER V3,0,0,3323  Xkisjd                 2014 02 25 05:23    PGM / RUN BY / DATE 
VER V1,2,4,0003  MfgHJd                 2015 12 11 11:13    PGM / RUN BY / DATE "; 
preg_match_all($re, $str, $matches);
$cnt = count($matches[0]);
$arr = array();
for ($i = 0; $i < $cnt; $i++) {
    $arrAdd = array();
    $arrAdd[$matches[4][$i].trim()] = $matches[1][$i];
    $arrAdd[$matches[5][$i]] = $matches[2][$i];
    $arrAdd[$matches[6][$i]] = $matches[3][$i];
    array_push($arr, $arrAdd);
}
print_r($arr);

Result:

Array
(
    [0] => Array
        (
            [PGM] => DER V3,0,0,3323
            [RUN BY] => Xkisjd
            [DATE] => 2014 02 25 05:23
        )

    [1] => Array
        (
            [PGM] => VER V1,2,4,0003
            [RUN BY] => MfgHJd
            [DATE] => 2015 12 11 11:13
        )

)