Articles

Split in PHP: What is str_split() function

by Edureka Blogs **Edureka **is an online training provider with th

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 PHParticle will provide you detailed knowledge about the alternatives to the split function. 

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.

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:

1
2
3
<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:

1
2
3
<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:

1
2
3
<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.



Sponsor Ads


About Edureka Blogs Junior   **Edureka **is an online training provider with th

1 connections, 0 recommendations, 7 honor points.
Joined APSense since, July 8th, 2019, From Bangalore, India.

Created on Jul 17th 2019 00:31. Viewed 434 times.

Comments

No comment, be the first to comment.
Please sign in before you comment.