split a string into an array of substrings in php

To split a string into an array of substrings in PHP, you can use the explode() function. This function splits a string into an array of substrings based on a specified delimiter.

Here's an example:

main.php
$string = "apple, banana, cherry, date";
$array = explode(", ", $string);

print_r($array);
92 chars
5 lines

Output:

main.php
Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
    [3] => date
)
79 chars
8 lines

In this example, we have a string that contains four fruits separated by commas and spaces. We pass the delimiter ", " (comma and space) to the explode() function, which splits the string into an array of substrings.

The resulting array contains each fruit as a separate element.

Note: The explode() function is case-sensitive, so make sure the delimiter you pass to it matches the one in your string.

gistlibby LogSnag