开发者

Using preg_match_all to create chord charts

开发者 https://www.devze.com 2023-02-10 12:49 出处:网络
I want to create a PDF chord chart when someone enters lyrics. The format the lyrics will be entered are like this:

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开发者_Go百科 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.

<?php
define('REGEX_CHORD', '[A-G].*?');

$input = '[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';

$lines = explode("\n", $input);
echo '<pre>';
foreach($lines as $line) {
    $chordline = '';
    $tmpline = $line;
    while(preg_match('~(.*)\[('.REGEX_CHORD.')\]~', $tmpline, $matches)) {
        // Length before the chord shows how many spaces are required.
        $chordline = str_repeat(' ', strlen($matches[1]))
                .$matches[2]
                .substr($chordline, 
                    strlen($matches[2]) + 3 + strlen($matches[1]));
        $tmpline = $matches[1];
    }
    echo $chordline."\n";
    echo preg_replace('~\[('.REGEX_CHORD.')\]~', '', $line)."\n";
}


I think this is the preg_match you're looking for:

/(\[[A-G].*?\])/

It captures any chord and following symbols that are encapsed by square brackets. It even matches flats, sharps, and modifiers like below:

[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


[Bb]Some lyrics blah blah [Dm7+9]more lyrics blah
Lyrics without any chord charts blah blah
Some lyrics [C]blah blah more [Eb]lyrics blah

See a working example here: http://regexr.com?2t408

0

精彩评论

暂无评论...
验证码 换一张
取 消