I know that the split
function is deprecated and there is the explode
function that replaces it, but what exactly makes explode
better?
I know that the split
function is deprecated and there is the explode
function that replaces it, but what exactly makes explode
better?
Split is a function of the older versions of PHP, which did something similar to explode
, with the use of regular expressions as a difference.
I can not remember the reference now, but this function was six times slower than explode
.
Now, we have to take into account that both are similar, not identical. Explode works with division by delimiter indicated by string
, already split
, regular expression.
Note that explode
did not replace the split
function, but the preg_split
function.
The use of preg_split
is encouraged instead of split
See an example:
$parts = preg_split('/,|\./', 'teste.teste,teste');
This generates:
['test', 'test', 'test']
And answering directly the question What explode does better than split ?
Failure to use regular expression can mean a lot. Not always when we need to split a string to a array
we need a regular expression. Hence the idea of using explode
instead of split
.
And even though preg_split
is the substitute for split
, in cases where the string is not strictly parsed, it is encouraged to use explode
instead.