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.
<?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 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().
<?php $date_array = preg_split("/[\/\.-]+/", date('d-m-y')); print_r($date_array); ?>
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.
If we are needed to match with some regex pattern, preg_split() will be a best alternative to split().
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().
<?php $welcome_message = "Welcome to php environment"; $word_array = explode( ' ', $welcome_message ); print_r($word_array); ?>
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.
Array (  => Welcome  => to  => php  => environment )
This PHP code tutorial was published on April 13, 2013.