Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use preg_split to split chords and words

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 !

like image 205
gordie Avatar asked Dec 16 '15 10:12

gordie


1 Answers

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.

like image 103
Casimir et Hippolyte Avatar answered Oct 10 '22 15:10

Casimir et Hippolyte