What are some best practices for handling string manipulation tasks in PHP, especially when dealing with complex delimiters and multi-dimensional arrays?

When handling string manipulation tasks in PHP, especially with complex delimiters and multi-dimensional arrays, it is best to use built-in functions like explode(), implode(), and preg_split() to split and join strings based on specific delimiters. Additionally, using foreach loops to iterate through multi-dimensional arrays and performing string manipulation operations within the loop can help achieve the desired results efficiently.

// Example of handling string manipulation tasks with complex delimiters and multi-dimensional arrays

// Sample string with complex delimiters
$string = "apple,orange;banana|grape";

// Split the string into an array using multiple delimiters
$delimiters = [",", ";", "|"];
$parts = preg_split('/[' . preg_quote(implode('', $delimiters), '/') . ']/', $string);

// Iterate through the array and perform string manipulation tasks
foreach ($parts as $part) {
    echo strtoupper($part) . " "; // Example manipulation task
}