Why PHP split() is Deprecated

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 poor performance. So that, it was deprecated and removed from PHP. The following code shows an example for the split function with [– :] pattern to split the given string.

$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 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.

$date_array = preg_split("/[\/\.-]+/", date('d-m-y'));


It is good to know the difference between POSIX and PCRE to use regex in an efficient way. These are listed as follows.

  1. PCRE functions like preg_split(), preg_match and preg_replace should have patterns within a set of open and close delimiters.
  2. In POSIX extension, it includes separate functions for case sensitive and insensitive matching. Example split() and spliti(). The PCRE extension has common functions for both cases sensitive and insensitive match. But, we need to specify pattern modifier for representing case sensitivity. Example preg_split().
  3. In POSIX, it searches for longest pattern match on the target string. But, PCRE provides even substring match in an effective manner and so faster.

Splitting a String using PHP explode()

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().

$welcome_message = "Welcome to php environment";
$word_array = explode( ' ', $welcome_message );

The above code will split the given string into an array by the white space. The array will be,

    [0] => Welcome
    [1] => to
    [2] => php
    [3] => environment

This PHP code tutorial was published on April 13, 2013.

↑ Back to Top