I'm working on a little piece of code playing handling song tabs, but i'm stuck on a problem.
I need to parse each song tab line and to split it to get chunks of chords on the one hand, and words in the other.
Each chunk would be like :
$line_chunk = array(
0 => //part of line containing one or several chords
1 => //part of line containing words
);
They should stay "grouped". I mean by this that it should split only when the function reaches the "limit" between chords and words.
I guess I should use preg_split to achieve this. I made some tests, but I've been only able to split on chords, not "groups" of chords:
$line_chunks = preg_split('/(\[[^]]*\])/', $line, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
Those examples shows you what I would like to get :
on a line containing no chords :
$input = '{intro}';
$results = array(
array(
0 => null,
1 => '{intro}
)
);
on a line containing only chords :
$input = '[C#] [Fm] [C#] [Fm] [C#] [Fm]';
$results = array(
array(
0 => '[C#] [Fm] [C#] [Fm] [C#] [Fm]',
1 => null
)
);
on a line containing both :
$input = '[C#]I’m looking for [Fm]you [G#]';
$results = array(
array(
0 => '[C#]',
1 => 'I’m looking for'
),
array(
0 => '[Fm]',
1 => 'you '
),
array(
0 => '[G#]',
1 => null
),
);
Any ideas of how to do this ?
Thanks !