What are best practices for handling multiple delimiters in PHP when using explode?
When using the explode function in PHP to split a string by delimiters, you can handle multiple delimiters by first replacing all delimiters with a single delimiter, and then using explode with that single delimiter. This can be achieved using the str_replace function to replace all delimiters with a single delimiter before using explode.
$string = "apple,orange;banana|grape";
$delimiters = [",", ";", "|"];
$single_delimiter = "~";
foreach ($delimiters as $delimiter) {
$string = str_replace($delimiter, $single_delimiter, $string);
}
$exploded_array = explode($single_delimiter, $string);
print_r($exploded_array);