Skip to content
PHP Logo

How can the explode function in PHP be used to split a string based on a specific character?

To split a string based on a specific character in PHP, you can use the explode function. The explode function takes two parameters - the delimiter character you want to split the string by and the string itself. It then returns an array of substrings that were separated by the delimiter character.

php
$string = "apple,banana,orange";
$delimiter = ",";
$fruits = explode($delimiter, $string);

foreach ($fruits as $fruit) {
    echo $fruit . "<br>";
}