Why PHP split() is Deprecated

In PHP, split() was used to split a string into an array by the given regex pattern. It performed case sensitive pattern matching on the given string. Since regex parsing has shows poor performance, this function was deprecated. An example code snippet to split string with [– :] pattern is shown below.

$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. Instead of split(), there is an alternate function named as preg_split() under PCRE extension which has extensive matching capability than split().

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

The code uses preg_split() function. In preg_split(), pattern must be enclosed by #, / and etc.


There are some differences between POSIX and PCRE to be known to use regex in effective manner. These are listed as follows.

  1. PCRE functions like preg_split(), preg_match and preg_replace should have patterns within 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 case 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 provide even sub string match in effective manner and so faster.

If we are needed to match with some regex pattern, preg_split() will be a best alternative to split().

Split using explode()

explode() which is an alias of split(). It matches string with the sub string instead of regex pattern. So there is no need of any regex parser during the execution of explode(). And it is faster than both split() and preg_split().

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

The above code will split the string into an array by splitting the word for each white space. And the output will be as follows.

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

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

↑ Back to Top