Split in PHP: What is str_split() function

Last updated on Jun 14,2023 6.7K Views
A Data Science Enthusiast with in-hand skills in programming languages such as... A Data Science Enthusiast with in-hand skills in programming languages such as Java & Python.

Split in PHP: What is str_split() function

edureka.co

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:

Split in PHP

The split function was disapproved in PHP 5.3.0 and removed in PHP 7. There are alternatives to this function and they are:

What is preg_split()?

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. Check out this full stack developer course to learn more about PHP today.

Syntax:

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>

 

Output:

Array
(
    [0] => ashok
    [1] => tarun
    [2] => charan
    [3] => sabid
)

In this way we split a string into component characters:

Output:

Array
(
    [0] => a
    [1] => s
    [2] => h
    [3] => o
    [4] => k
)

In this way, we split a string into matches and their offsets:

Output:

Array
(
    [0] => Array
        (
            [0] => ashok
            [1] => 0
        )

    [1] => Array
        (
            [0] => is
            [1] => 6
        )

    [2] => Array
        (
            [0] => a
            [1] => 9
        )

    [3] => Array
        (
            [0] => student
            [1] => 11
        )

)

What is explode()?

It is a built-in function which breaks a string into an array.

Syntax:

explode(separator,string,limit)

Example:

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

Output:

Array
(
    [0] => sunday,monday,tuesday,wednesday
)
Array
(
    [0] => sunday
    [1] => monday,tuesday,wednesday
)
Array
(
    [0] => sunday
    [1] => monday
    [2] => tuesday
)

What is str_split()?

This function is used for splitting a string into an array using length parameter.

Syntax:

str_split(string,length()).

Example:


<pre>
<?php print_r(str_split("ashok",4)); ?>
</pre>

Output:

Array
(
    [0] => asho
    [1] => 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.

Check out the Angular Course by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Angular is a JavaScript framework that is used to create scalable, enterprise, and performance client-side web applications. With Angular framework adoption being high, performance management of the application is community-driven indirectly driving better job opportunities.

Got a question for us? Please mention it in the comments section of ”split in PHP” and I will get back to you.

BROWSE COURSES
REGISTER FOR FREE WEBINAR A Comprehensive UI/UX Design Tutorial