I'm parsing custom data that derives from .chart
files.
Here's a snippet of what the syntax is like:
[Song]
{
Player2 = bass
Difficulty = 0
PreviewStart = 0.00
PreviewEnd = 0.00
Genre = "rock"
}
[SyncTrack]
{
0 = B 125000
0 = TS 1
768 = TS 4
}
[Events]
{
0 = E "section Verse I"
19200 = E "section Break I"
25344 = E "section Verse II"
31488 = E "section Verse III"
37632 = E "section Orchestral Bridge 1"
43776 = E "section Break II"
49920 = E "section Chord Hero I"
}
[ExpertSingle]
{
768 = N 2 192
1056 = N 3 192
1344 = N 4 168
1536 = N 2 192
1824 = N 1 192
2112 = N 0 168
}
I've attempted to parse this data. Here's a snippet of my messy code so far:
$music_key_data = explode('[ExpertSingle]', $data);
$music_key_data = str_replace('}', '', $music_key_data);
$music_key_data = str_replace('{', '', $music_key_data);
$music_key_data = array_filter(preg_split ('/$\R?^/m', $music_key_data[1]));
foreach ($music_key_data as $var) {
$music_data = explode(' ', $var);
$data1 = $music_data[2];
$data2 = $music_data[5];
$data3 = $music_data[6];
}
How should I go about parsing this array into something easy to use in PHP? What I've done so far all seems like too much of a mess
Am I going about this the right way or is there a better method for parsing data like this?
Thanks
EDIT:
Another attempt:
$data = array_filter(preg_split ('/$\R?^/m', $data));
$array = array();
$inside_array = false;
foreach ($data as $var) {
if ($inside_array === true && $var !== '{' && $var !== '}') {
if (strpos($var, '=') !== false) {
$var_explode = explode('=', $var);
$array[$parent_name][ trim($var_explode[0]) ][] = trim($var_explode[1]);
} else {
$array[$parent_name][] = $var;
}
}
if (strpos($var, '[') !== false && strpos($var, ']') !== false) {
$parent_name = str_replace(array('[', ']'), array('', ''), $var);
$inside_array = true;
}
}
print_r($array);
die;