6

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 !

gordie
  • 1,035
  • 2
  • 12
  • 25

2 Answers2

1

preg_split isn't the way to go. Most of the time, when you have a complicated split task to achieve, it's more easy to try to match what you are interested by instead of trying to split with a not easy to define separator.

A preg_match_all approach:

$pattern = '~ \h*
(?|        # open a "branch reset group"
    ( \[ [^]]+ ] (?: \h* \[ [^]]+ ] )*+ ) # one or more chords in capture group 1
    \h*
    ( [^[\n]* (?<=\S) )  # eventual lyrics (group 2)
  |                      # OR
    ()                   # no chords (group 1)
    ( [^[\n]* [^\s[] )   # lyrics (group 2)
)          # close the "branch reset group"
~x';

if (preg_match_all($pattern, $input, $matches, PREG_SET_ORDER)) {
    $result = array_map(function($i) { return [$i[1], $i[2]]; }, $matches);
    print_r($result);
}

demo

A branch reset group preserves the same group numbering for each branch.

Note: feel free to add:

if (empty($i[1])) $i[1] = null;    
if (empty($i[2])) $i[2] = null;

in the map function if you want to obtain null items instead of empty items.

Note2: if you work line by line, you can remove the \n from the pattern.

Casimir et Hippolyte
  • 83,228
  • 5
  • 85
  • 113
0

I would go with PHP explode:

/*
 * Process data
 */
$input = '[C#]I’m looking for [Fm]you [G#]';
$parts = explode("[", $input);
$results = array();

foreach ($parts as $item)
{
    $pieces = explode("]", $item);

    if (count($pieces) < 2)
    {
        $arrayitem = array( "Chord" => $pieces[0],
                            "Lyric" => "");
    }
    else
    {
        $arrayitem = array( "Chord" => $pieces[0],
                            "Lyric" => $pieces[1]);
    }

    $results[] = $arrayitem;
}

/*
 * Echo results
 */
foreach ($results as $str)
{
    echo "Chord: " . $str["Chord"];
    echo "Lyric: " . $str["Lyric"];
}

Boudaries are not tested in the code, as well as remaining whitespaces, but it is a base to work on.

Mendes
  • 13,757
  • 24
  • 122
  • 217