PHP Split String | How to Split String in PHP with Example?

Summary: In this article, we demonstrate and explore PHP split string function and how to split string in PHP? Let’s understand in detail this Function with an example.

PHP Split Function Definition and Usage

The split() function, split the string into an array by regular expression or splits a string into an array.

The str_split() function will divide a string into various elements, the boundaries of every element, it is based on the occurrence of a pattern in a string.

The optional input parameter limit is utilized to indicate the number of elements into that the string must be divided, beginning from the left end of the string and working rightward.

.
Important Note: where the pattern is an alphabetical character, then split() is case sensitive.

Syntax:

split ( string $pattern , string $string , int $limit = -1 ) : array

Parameter Value

Parameter Value Description
pattern Case sensitive regular expression.
If you want to split on any of the characters, then they are considered special by regular expressions, furthermore, you will require to escape them first. furthermore, if you are thinking that split() (or any other regex function, for that matter) is doing something weird
string The input string.
limit If the limit is set, then the returned array will contain a maximum of limit elements with the last element containing the entire rest of the string.

Return Values

Returns an array of strings, every of that could be a substring of string formed by splitting it on boundaries formed by the case-sensitive regular expression pattern.

Suppose that, if there are n occurrences of pattern, then the returned array will contain n+1 items. as an example, if there’s no occurrence of pattern, then an array with only one(1) element will be returned. Of course, this is also true if the string is empty. If an error occurs, then the split() returns false.

PHP split() Example

Program:1

Array (
[0] => W
[1] => E
[2] => L
[3] => C
[4] => O
[5] => M
[6] => E
[7] =>
[8] => T
[9] => O
[10] =>
[11] => U
[12] => S
[13] => A )
Program:2

Array (
[0] => WE
[1] => LC
[2] => OM
[3] => E
[4] => TO
[5] => U
[6] => SA )

PHP Split Related Question Answer

Question: What is preg_split()?

Answer: In PHP, this is an inbuilt function that is used to convert the given string into an array. furthermore, the string gets split into smaller sub-strings of length that are specified by the user using the preg_split() function. Let’s say, the limit is described then sub-strings return through an array up to limit.

Syntax:

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

If you wish to split the phrase by any number of commas or space characters :

Array (
[0] => aditya
[1] => pinki gupta
)

Conclusion:

In this article, you have learned to PHP split string function and the way to split string in PHP. also you have learned about preg_split() function. I hope you will enjoy it!. if have any query related program then contact me at info@tutorialscan.com I will try to resolve the problem. Stay healthy, stay safe! Happy coding! enjoy it!