PHP and MySQL (56 Blogs) Become a Certified Professional

Split in PHP: What is str_split() function

Last updated on Jun 14,2023 6.5K 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

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:

  • preg_split()
  • explode()
  • str_split().

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)

  • pattern: It is of string type for searching the pattern else it separates the elements.
  • subject: It is a variable that is used to store the input string.
  • limit: It indicates the limit. If the limit is specified, then sub-string has to be returned up to limit. If the limit is 0 or -1, it indicates “no limit” which is used by the flag.
  • flag:
    PREG_SPLIT_NO_EMPTY − Only non-empty pieces will be returned by preg_split()
    PREG_SPLIT_DELIM_CAPTURE − Parenthesized expression in the delimiter pattern will be captured and returned as well.
    PREG_SPLIT_OFFSET_CAPTURE − For every occurring match the appendant string offset will also be returned.

 

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)

  • Separator: This character decides the string to split.
  • String: This input string will split into an array.
  • Limit: This is optional which specifies the number of elements in an array.

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

  • String: It specifies the string to split.
  • Length: It specifies the length of every individual array element. Default is 1.

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.

Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Split in PHP: What is str_split() function

edureka.co