0

I would like to ask for help please.

I got this syntax

[86855,[[6615663,"Name 1 [GBR]","Name 2 [RUS]",86855,"2017-01-13T09:30:00.0000000",[[16942762,0,,,,1],[16942763,738,,,,2],[16942869,741,,,,1],[16942870,113,,,,1],[16944801,759,,,1],[16944804,759,,,2],[16943142,740,,,,1],[16943144,743,,,,5],[16943145,744,,,,5]],[[25262023,758],[25259130,748],[25259131,749],[25258578,739]],,,,,80104,194132,-1,-1,0,0,0,0,"Sport","League",6,22,0,null,"10890680"],[6614528,"Name 1 [SRB]","Name 2 [LUX]",86855,"2017-01-13T05:00:00.0000000",[[16939629,741,,,,1],[16939630,113,,,,1],[16939632,759,,,1],[16939634,759,,,2],[16939069,0,,,,1],[16939070,740,,,,1],[16939071,738,,,,2],[16942414,743,,,,5],[16942415,744,,,,5]],[[25257607,748],[25257608,749],[25257609,758],[25253011,739]],,,,,1610,48295,-1,-1,0,0,0,0,"Sport","League",6,22,0,null,"10888520"]],[[25138199,8,,,,"2017-01-13T05:00:00.0000000",,,,0,null,null]]],[]

And i would like to get array from output. Something like this

[0] => 86855
  [0] =>
     [0] => 6615663
     [1] => "Name 1 [GRB]"
     etc...
  [1] =>
     [0] => 6614528
     [1] => "Name 1 [SRB]"
     etc...

Is it possible to do that by any reg. expression or parse somehow?

Thank you :)

Ernesto
  • 9
  • 2

1 Answers1

0

Your data looks like an array enclosed in a string. You can build an array using eval but, for security reasons, you need to check if your string contains only allowed items (ie: numeric values, strings, empty items, null, and arrays. In other words, no executable code) and if it is well formatted.

In a second time, you need to change "empty" values to something that the PHP parser is able to recognize.

Example:

$pattern = '~
    (?(DEFINE)                         # tokens definitions
        (?<quoted> " [^"\\\\]*+ (?s:\\\\.[^"\\\\]*)*+ " )
        (?<num> -? \d+ (?: \. \d*)? | \. \d+ )
        (?<item> \g<quoted> | \g<num> | \g<array> | null | \s* )
        (?<itemList> \g<item> (?: \s* , \s* \g<item> )* )
        (?<array> \[ \s* \g<itemList> \s* ] )
    )

    \A \s* \g<itemList> \s* \z         # main pattern
~xi';

if (preg_match($pattern, $str)) {
    $expr = preg_replace('~"[^"\\\\]*+(?s:\\\\.[^"\\\\]*)*+"(*SKIP)(*F)|,\s*(?=,)~S', ',null', $str);
    $expr = '$result=[' . $expr . '];'; 
    eval($expr);
    print_r($result);
} else throw new Exception('Bad format');
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
  • Oh my god dude, This is awesome! Exactly what i needed, you helped me a lot :) Thank you very much! :) It works. – Ernesto Jan 13 '17 at 07:38