How to split a string with multiple separators in PHP

1 Answer

0 votes
$input = "abc,defg;hijk|lmnop-qrst_uvwxyz";

// Match multiple separators: comma, semicolon, pipe, dash, underscore
$result = preg_split("/[,\;\|\-_]/", $input);

foreach ($result as $word) {
    echo $word . PHP_EOL;
}



/*
run:

abc
defg
hijk
lmnop
qrst
uvwxyz

*/

 



answered Jul 21, 2025 by avibootz
...