Angular Certification Training
- 16k Enrolled Learners
- Live Class
A scripting language is a language that interprets scripts at runtime. The purpose of the scripts is usually to enhance the performance or perform routine tasks for an application. This split in PHP article will provide you detailed knowledge about the alternatives to the split function in the following sequence:
The split function was disapproved in PHP 5.3.0 and removed in PHP 7. There are alternatives to this function and they are:
It is an inbuilt function in PHP which is used to convert the given string into an array. The string gets split into smaller sub-strings of length which is specified by the user using this function. Let’s say, the limit is specified then sub-strings return through an array up to limit.
array preg_split(pattern,subject,limit, flag)
If you want to split the phrase by any number of commas or space characters:
<pre> <?php $variable = preg_split("/[s,]+/", "ashok tarun, charan sabid"); print_r($variable); ?> </pre>
Array (  => ashok  => tarun  => charan  => sabid )
In this way we split a string into component characters:
Array (  => a  => s  => h  => o  => k )
In this way, we split a string into matches and their offsets:
Array (  => Array (  => ashok  => 0 )  => Array (  => is  => 6 )  => Array (  => a  => 9 )  => Array (  => student  => 11 ) )
What is explode()?
It is a built-in function which breaks a string into an array.
<pre> <?php $var_str = 'sunday,monday,tuesday,wednesday'; print_r(explode(',',$var_str,0)); //zero limit print_r(explode(',',$var_str,2));// positive limit print_r(explode(',',$var_str,-1));// negative limit ?> </pre>
Array (  => sunday,monday,tuesday,wednesday ) Array (  => sunday  => monday,tuesday,wednesday ) Array (  => sunday  => monday  => tuesday )
This function is used for splitting a string into an array using length parameter.
<pre> <?php print_r(str_split("ashok",4)); ?> </pre>
Array (  => asho  => k )
With this we come to an end of this article, I hope you have learned about all the three split functions preg_split(), explode(), str_split() with examples.
If you found this split in PHP blog relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
Got a question for us? Please mention it in the comments section of ”split in PHP” and I will get back to you.