PHP split() in PHP is deprecated as of its version 5.3.0 and it is removed as of 7.0.0. It was used for splitting a string into an array by the specified regex pattern. It parsed the string for the regex pattern in case sensitive manner.
This kind of regex parsing leads to poor performance. So, it was deprecated and removed from PHP. The following code shows an example of the split function with [– :] pattern to split the given string.
<?php
$str = "yyyy-mm-dd HH:ii:ss";
split('[- :]', $str);
?>
split() function is coming under the POSIX regex extension. But as of 5.3.0, this extension is replaced by PCRE(Perl Compatible Regular Expression) extension which leads to extended regex features.
So, if we use this function in later versions of PHP greater than 5.3.0 will create E_DEPRECATED warning notice. There are some alternate functions in PHP for splitting a string into an array.
Those are, preg_split(), str_split() and explode(). The code uses preg_split() to split the date string by using the given regex format.
<?php
$date_array = preg_split("/[\/\.-]+/", date('d-m-y'));
print_r($date_array);
?>
It is good to know the difference between POSIX and PCRE to use regex in an efficient way. These are listed as follows.
The explode() is an alias of split() function and uses string instead of regex pattern for splitting. So there is no need of any regex parser during the execution of explode().
And it is faster than both split() and preg_split(). The following code shows the PHP program to split a string using explode().
<?php
$welcome_message = "Welcome to php environment";
$word_array = explode(' ', $welcome_message);
print_r($word_array);
?>
The above code will split the given string into an array by the white space. The array will be,
Array
(
[0] => Welcome
[1] => to
[2] => php
[3] => environment
)
Thanks for the info.
Welcome Jose.