- preg_match_all: documentation ( source)
- trim: documentation ( source)
- join: documentation ( source)
<?php
$text = '
Balthazar - True Love # Maarten Devoldere
Adani & Wolf - Let It Go # Daniel Testas
Adani & Wolf - Let It Go # Rob Gaasterland
Balthazar - True Love # Maarten Devoldere
Balthazar - True Love # Jinte Deprez
Balthazar - True Love # Name 3
';
preg_match_all('/^([^#]+)#([^\n]+)/m', trim($text), $matches, PREG_SET_ORDER);
foreach ($matches as $v) {
$result[trim($v[1])][] = trim($v[2]);
}
foreach ($result as $k => $v){
echo $k .' # '. join(', ', $v) . PHP_EOL;
}