What is the difference between explode and preg_split in PHP?
The main difference between explode and preg_split in PHP is that explode splits a string based on a simple delimiter (such as a comma or space), while preg_split allows for more complex splitting using a regular expression pattern. If you need to split a string based on a simple character or string, you can use explode. However, if you need more flexibility in defining the splitting pattern, preg_split is the better choice.
// Using explode to split a string based on a simple delimiter
$string = "apple,orange,banana";
$fruits = explode(",", $string);
print_r($fruits);
// Using preg_split to split a string based on a regular expression pattern
$string = "apple orange banana";
$words = preg_split("/\s+/", $string);
print_r($words);