How do you split a string by a specified delimiter in PHP With Example?

Hello Friends Today, through this tutorial, I will tell you How do you split a string by a specified delimiter in PHP With example? In PHP, you can split a string into an array of substrings using the `explode()` function. This function takes two parameters: the delimiter (a string indicating where to split the original string) and the original string itself. Here’s an example:

<?php
$string = "apple,banana,orange,grape";
$delimiter = ",";
// Split the string by the delimiter
$array = explode($delimiter, $string);
// Output the resulting array
print_r($array);
?>

Output:

Array
(
[0] => apple
[1] => banana
[2] => orange
[3] => grape
)

In this example, the string `$string` contains a list of fruits separated by commas (`,`). We use `explode()` to split the string into an array using the comma (`,`) as the delimiter. The resulting array `$array` contains each fruit as a separate element.