I'm working on a song database application with an existing database. In the editor the songs are laid out like this in the contents
column:
[chorus]
And all will surely agree,
There's nothing like PHP.
[1]
It may not be the best way,
But it isn't going away.
[2]
When you don't appear to hack it,
Check for a closing bracket.
[bridge]
Oh, if you don't like this ode,
Then please don't look at my code.
The sequence
column then contains the order of the song which for this might be 1,c,2,c,b,1,c
(where the numbers are the verse numbers and b and c are the bridge and chorus).
I want to read the database and assemble the song into the right order with a blank line between sections. I figured the best way was to split the song into an associative array and then loop through the sequence adding the associated parts to the output.
How should I do this? I thought that the parse_ini_string()
function might make it easy as the section heads are in [ ]
but I suspect that it's looking for var=something;
on each line after the section head.
How best could I do this?
Notes:
I would use another associative array to match the section heads to their sequence abbreviations.
$sequenceCodes = array(
"1" => "verse 1",
"2" => "verse 2",
"3" => "verse 3",
...
"p" => "prechorus",
"c" => "chorus",
"b" => "bridge",
"e" => "ending",
);
[...]
, start a new array element with that key. If not, concatenate the line onto the current array element.$sequenceCodes
array doesn't really match the data. For the verses, the keys correspond to what's in[]
in the text. But for the named sections, the names correspond to the values.\[[^\]]*\]
and see how I get on. I haven't started debugging the $sequenceCodes yet but I should be able to fix it. Post your comment as an answer and I'll accept it (subject to testing in the morning!).