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",
);
This is a wicked, wicked XY Problem that should be refactored because you've forced yourself to tackle a problem which is harder than it should be.
It would be more ideal to store your contents data in a normalized fashion or a more simply parsed format. That said, here is a regex parsing approach to extract the first character of labels then match subsequent lines, then strtr()
will render the sequence output. Demo
$contents = <<<INI_ISH
[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.
INI_ISH;
preg_match_all(
'#^\[([^\]])[^\]]*\]\R\K(?:(?!^\[).)*$#ms',
$contents,
$m
);
$map = array_combine($m[1], $m[0]);
$map[','] = "\n\n";
$sequence = '1,c,2,c,b,1,c';
echo strtr($sequence, $map);
Output:
It may not be the best way,
But it isn't going away.
And all will surely agree,
There's nothing like PHP.
When you don't appear to hack it,
Check for a closing bracket.
And all will surely agree,
There's nothing like PHP.
Oh, if you don't like this ode,
Then please don't look at my code.
It may not be the best way,
But it isn't going away.
And all will surely agree,
There's nothing like PHP.