0

Hi I have a CSV file with this structure:

A243503  ;H ;ZU, HS-JIM ;7;F122;VO ;A12 ;A2E;U;be;24.11.15 ;05.12.15 ;11^/
; ; 1 ;   1 ;A26 ;24.11.15 ;11:05;14:45;TL;F ;^/
18.10.15 ;18.10.15 ;BU;
    BLABLABLABLA BLABLABLABLA BLABLABLABLA
A243504;F  ;BAS, R   56  ;FC12VOp ;A12;App 2E; ;ern;24.11.15 ;05.12.15;11 ^/
 ;  ; 1 ;2  ;A26;24.11.15  ;11:05 ;14:45 ;TiXL  ;FC  ;   ^/
18.10.15 ;18.10.15 ;BU       ;
A243483 ;H ;BR, WG  ;64  ;F113 ;GR  ;DIS;DZ;H ;Hp ;19.04.16;28.04.16; 9       ^/
 ;  ; 1  ;   1  ;A306   ;19.04.16    ;09:50    ;13:30    ;TX ;NC;            ^/
18.10.15 ;18.10.15 ;BU  ;
    Fr1%
    il. Rg ud Tr a/b

And I need to divide it at each A999999. So I created the following code to do it:

$re = "/(\sA\d{6})/"; 
$result= preg_split($re , $str,-1,PREG_SET_ORDER);
print_r($result);

However what is printed is:

Array
(
    [0] => A243503  ;H ;ZU, HS-JIM ;7;F122;VO ;A12 ;A2E;U;be;24.11.15 ;05.12.15 ;11^/
    ; ; 1 ;   1 ;A26 ;24.11.15 ;11:05;14:45;TL;F ;^/
    18.10.15 ;18.10.15 ;BU;
        BLABLABLABLA BLABLABLABLA BLABLABLABLA

    [1] =>  A243504
    [2] => ;F  ;BAS, R   56  ;FC12VOp ;A12;App 2E; ;ern;24.11.15 ;05.12.15;11 ^/
     ;  ; 1 ;2  ;A26;24.11.15  ;11:05 ;14:45 ;TiXL  ;FC  ;   ^/
    18.10.15 ;18.10.15 ;BU       ;

    [3] =>  A243483
    [4] =>  ;H ;BR, WG  ;64  ;F113 ;GR  ;DIS;DZ;H ;Hp ;19.04.16;28.04.16; 9       ^/
     ;  ; 1  ;   1  ;A306   ;19.04.16    ;09:50    ;13:30    ;TX ;NC;            ^/
    18.10.15 ;18.10.15 ;BU  ;
        Fr1%
        il. Rg ud Tr a/b
)

But I wanted it to be something like:

Array
(
    [0] => A243503  ;H ;ZU, HS-JIM ;7;F122;VO ;A12 ;A2E;U;be;24.11.15 ;05.12.15 ;11^/
    ; ; 1 ;   1 ;A26 ;24.11.15 ;11:05;14:45;TL;F ;^/
    18.10.15 ;18.10.15 ;BU;
        BLABLABLABLA BLABLABLABLA BLABLABLABLA

    [1] =>  A243504 ;F  ;BAS, R   56  ;FC12VOp ;A12;App 2E; ;ern;24.11.15 ;05.12.15;11 ^/
     ;  ; 1 ;2  ;A26;24.11.15  ;11:05 ;14:45 ;TiXL  ;FC  ;   ^/
    18.10.15 ;18.10.15 ;BU       ;

    [2] =>  A243483 ;H ;BR, WG  ;64  ;F113 ;GR  ;DIS;DZ;H ;Hp ;19.04.16;28.04.16; 9       ^/
     ;  ; 1  ;   1  ;A306   ;19.04.16    ;09:50    ;13:30    ;TX ;NC;            ^/
    18.10.15 ;18.10.15 ;BU  ;
        Fr1%
        il. Rg ud Tr a/b
)

I want the matched text to go together with the rest..

  • 1
    Possible duplicate of [Is there way to keep delimiter while using php explode or other similar functions?](http://stackoverflow.com/questions/2938137/is-there-way-to-keep-delimiter-while-using-php-explode-or-other-similar-function) – Cᴏʀʏ Oct 26 '15 at 13:02
  • I tried this here: https://3v4l.org/ZoOEV. Didn´t work – Miguel Sousa Oct 26 '15 at 13:08
  • 2
    @MiguelSousa According to that link `Then you can take each pair of 2‍n and 2‍n+1 and put them back together` – apokryfos Oct 26 '15 at 13:15
  • Yes, thank you. But I wanted to find a cleaner way, with regex and split that worked... – Miguel Sousa Oct 26 '15 at 13:18

2 Answers2

1

With this pattern:

$result = preg_split('~\s*(?=A[0-9]{6})~', $str, -1, PREG_SPLIT_NO_EMTPY);

details:

~             # pattern delimiter
\s*           # trailing whitespaces of the previous occurrence (including newline)
(?=A[0-9]{6}) # lookahead: followed by "A" and 6 digits
~
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
0

Try it this way. I cant test it myself so I am not sure it works:

$re = "/(\sA\d{6})/"; 
$result= preg_split($re , $str);
print_r($result);
Ephedra
  • 831
  • 10
  • 24