How to split a string by a substring delimiter in PHP

1 Answer

0 votes
$str = "Apples and Oranges and Bananas and Grapes are tasty";
$delimiter = "and";

// Split the string by the delimiter
$parts = explode($delimiter, $str);

foreach ($parts as $part) {
    echo trim($part) . "\n";
}



/*
run:

Apples
Oranges
Bananas
Grapes are tasty

*/

 



answered Aug 11, 2025 by avibootz

Related questions

1 answer 125 views
2 answers 286 views
1 answer 69 views
...