How to Answer: What is the difference between explode() and preg_split() in PHP?
Advice and answer examples written specifically for a PHP Basic Concepts job interview.
3. What is the difference between explode() and preg_split() in PHP?
The difference between explode() and preg_split() is that explode() just breaks a string using a separator but preg_split() breaks a string using a regular expression.
These functions examine your understanding of manipulating data in PHP and also the use of regular expressions.
Let's see an example below that demonstrates this issue in the code.
<?php
$friends= "Tom, Jack, Henry, Nicholas";
// using explode()
$frienda_array= explode(", ", $friends);
echo "<pre>";
print_r($frienda_array);
echo "</pre>";
// using preg_split()
$data= preg_split("/, /", $friends);
echo "<pre>";
print_r($data);
echo "</pre>";
?>
Written by Sujal khatiwada on May 4th, 2021