preg_split 带逗号的数组分隔符不拆分数组

preg_split with array delimiters with comma not split the array

我有一个数组,数组中有一个列表,我必须拆分才能找到下一个值

$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>';
    }
}

现在输出是 $artista_artisti 由分隔符

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

怎么了? 最后一行必须是

J BALVIN
DEN HARROW

为什么逗号不被识别? 谢谢

正则表达式 ~ 分隔符附近的周围白色 space 会干扰 ,,因为它需要尾随 space。您可以将 spaces 放在需要它们的定界词周围,并从外部正则表达式 ~.

中删除 spaces
// 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)) . ')~';
//--------------------------^^^

这会产生如下输出:

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>

如有必要,您可以在附加 <br> 之前 trim() 各个值。