/**
* removeDuplicatesMultiDelimiter
*
* Removes duplicate items from a string using multiple delimiters.
* Uses preg_split to break the string by ANY of the provided delimiters.
*
* @param string $s The input string.
* @param array $delimiters List of delimiters, e.g. [",", "|", ";"]
* @return string Cleaned string with duplicates removed,
* rejoined using the FIRST delimiter.
*/
function removeDuplicatesMultiDelimiter(string $s, array $delimiters): string
{
// Build a regex character class from delimiters
// Example: [,\|;] → splits on comma, pipe, semicolon
$escaped = array_map('preg_quote', $delimiters);
$pattern = '/[' . implode('', $escaped) . ']+/';
// Split using ANY delimiter
$parts = preg_split($pattern, $s, -1, PREG_SPLIT_NO_EMPTY);
// Remove duplicates while preserving order
$unique = array_unique($parts);
// Reassemble using the FIRST delimiter
return implode($delimiters[0], $unique);
}
// Main
$s = "AAA|aaa,aAA;aaA|AAa,AAA;BBB|CCC,AAA;AAA,aaa|aaa";
// Run the function with multiple delimiters
$result = removeDuplicatesMultiDelimiter($s, [",", "|", ";"]);
echo $result;
/*
run:
AAA,aaa,aAA,aaA,AAa,BBB,CCC
*/