0

I have an array with a list in an array and I have to split to find next a value

$artista_inserito = 'DEN HARROW';
$tutti_artisti_data_ora = [
    ['time_artisti' => '18:31:00', 'artista_artisti' => 'LUIS RODRIGUEZ & DEN HARROW', 'data_artisti' => '2020-04-09'],
    ['time_artisti' => '18:32:00', 'artista_artisti' => 'J BALVIN', 'data_artisti' => '2020-04-09'],
    ['time_artisti' => '18:33:00', 'artista_artisti' => 'THE BLACK EYED PEAS VS. J BALVIN', 'data_artisti' => '2020-04-08'],
    ['time_artisti' => '18:34:00', 'artista_artisti' => 'THE BLACK EYED PEAS FT J BALVIN', 'data_artisti' => '2020-04-09'],
    ['time_artisti' => '18:35:00', 'artista_artisti' => 'J BALVIN, DEN HARROW', 'data_artisti' => '2020-04-09'],
];
//here a list of delimiter
$databaseDelimiters = array('FEAT', 'feat', 'FT', 'ft', '+', 'AND', 'and', 'E', 'e', 'VS', 'vs', 'FEAT.', 'feat.', 'FT.', 'ft.', 'VS.', 'vs.', ',', '&', 'X', 'x', ', ', ',');

$artistDelimiters = '~ (?:' . implode('|', array_map(function ($v) {
    return preg_quote($v, '~');
}, $databaseDelimiters)) . ') ~';

$artists = array_flip(preg_split($artistDelimiters, $artista_inserito));
$result = [];
$autore_duplicato_stringa = '';
foreach ($tutti_artisti_data_ora as $row) {
    foreach (preg_split($artistDelimiters, $row['artista_artisti']) as $artist) {
// print the output with every artist
        echo $artist . '<br>';
    }
}

at now the output is $artista_artisti split by delimiters

LUIS RODRIGUEZ
DEN HARROW
J BALVIN
THE BLACK EYED PEAS
J BALVIN
THE BLACK EYED PEAS
J BALVIN
J BALVIN, DEN HARROW

what's wrong? the last row must be

J BALVIN
DEN HARROW

why the comma is not recognized? thanks

16
  • 1
    What is $artista_inserito? Commented Feb 3, 2021 at 21:49
  • 2
    It is due to $artistDelimiters having spaces between the preg delimiters ~ and your list of split strings. ~ (?:FEAT|feat|FT|ft|\+|AND|and|E|e|VS|vs|FEAT\.|feat\.|FT\.|ft\.|VS\.|vs\.|,|&|X|x|, |,) ~ Commented Feb 3, 2021 at 21:54
  • 1
    So the delimiters have to have spaces around them, and there's no spaces around the ,. Commented Feb 3, 2021 at 21:55
  • 2
    I can make your code work if I change all the delimiters like 'FEAT' into ' FEAT ' with surrounding space and remove the outer spaces near the ~ Commented Feb 3, 2021 at 21:56
  • 1
    @pette Why not? Like he said, you can add the spaces where needed to the delimiters instead of putting them around all the delimiters. Commented Feb 3, 2021 at 22:00

1 Answer 1

1

The surrounding whitespace near the regex ~ delimiters is interfering with the , because it expects a trailing space. You can place spaces around the delimiting terms that require them, and remove spaces from the outer regex ~.

// Put spaces only where needed
$databaseDelimiters = array(' FEAT ',  ' feat ', ' FT ', ' ft ', ' + ', ' AND ', ' and ', ' E ', ' e ', ' VS ', ' vs ', ' FEAT. ', ' feat. ', ' FT. ',  ' ft. ', ' VS. ', ' vs. ', ',', '&', ' X ', ' x ', ', ', ',');

// Remove the outer spaces from the map function
$artistDelimiters = '~(?:' . implode('|', array_map(function ($v) {
//-------------------^^^
    return preg_quote($v, '~');
}, $databaseDelimiters)) . ')~';
//--------------------------^^^

This produces output like:

LUIS RODRIGUEZ <br> DEN HARROW<br>J BALVIN<br>THE BLACK EYED PEAS<br>J BALVIN<br>THE BLACK EYED PEAS<br>J BALVIN<br>J BALVIN<br> DEN HARROW<br>

You can trim() the individual values before appending the <br> if necessary.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.