I want to create a PDF chord chart when someone enters lyrics. The format the lyrics will be entered are like this:
[B]Some lyrics blah blah [D]more lyrics blah
Lyrics without any chord charts blah blah
Some lyrics [C]blah blah more [E]lyrics blah
I then want to extract each chord change so I can export and display it in a PDF like so:
B D
Some lyrics blah blah more lyrics blah
Lyrics without any chord charts blah blah
C E
Some lyrics blah blah more lyrics blah
I have everything else sorted. At the moment I just need to figure out how to extraxt all the text between each chord and the chord itself…
Here’s my current code:
$lyrics = '[B]Some lyrics blah blah [D]more lyrics blah
Lyrics without any chord charts blah blah
Some lyrics [C]blah blah more [E]lyrics blah';
$lyrics_html = '';
$x = 0;
$lyrics_lines = explode("\n", $lyrics);
// We've put each line into an array and now we will process each individually
foreach($lyrics_lines AS $lyrics_line) {
$x++;
if ($x > 1)
$lyrics_html .= '<br />';
// Check and see if an chords exist in this line
preg_match_all('/\[(\w{1,3})\]/i',
$lyrics_line,
$out);
if (!empty($out[0])) { // Found some chords, format it
print_r($out);
} else { // No chords found so just display the lyrics
$lyrics_html .= $lyrics_line;
}
}
echo $lyrics_html;
My following code selects the chords no worries
preg_match_all('/\[(\w{1,3})\]/i',
$lyrics_line,
$out);
But when I try to select text before and after the chords, I can’t seem to figure it out… I tried the following:
preg_match_all('/(\w+)\[(\w{1,3})\](\w+)/i',
$lyrics_line,
$out);
Any ideas what code I need so that I can select the chords and the lyrics so I can format them?
The sort of result that would be good is something like this:
Array
(
[0] => Array
(
[0] => [B]
[1] => [D]
)
[1] => Array
(
[0] => Some lyrics blah bla
[1] => more lyrics blah
)
)
Interesting problem… Here’s a possible solution. I used the regular expression you had, but also used it to see how many spaces were required to pad the chords.