PHP Split String Overview | Split String Into Array by Regular Expression

Summary: In this article, we demonstrate and explore PHP Split String Overview and also learn different way to split string into array by regular expression. Let’s understand in detail this Function with an example.

PHP Split String Definition and Usage

The PHP split string is one of all the fundamental operations for any programing language. There are various ways in PHP to deal with split relation operations. once we come back to understand the word split, we will experience the breaking of a single string into multiple strings. There are different ways we can split a string as per our needs.

Alternatives to this function include:

In alternative words, if you wish to split a string to get the string character by character (1 character each), by special character, by space, etc. We can also go with the size combination while handling or dealing with the string split.


explode(Separator, String, Limit)
  • Separator: A separator is a delimiter, which is the required parameter.
  • String: The string that required to be broken into multiple. Again, this is a required parameter.

The limit is an optional parameter.

  • String: This is a required parameter.
  • Length: This is an optional parameter.

RegularExpressionPattern- it is a required parameter.

  • String: it’s Required parameter.
  • Limit: Optional one.

The Function of PHP Split String

We should have a string initial to perform the operation on that string. After the string, we can come to the needed delimiter upon that the string split operation required to perform.

1. The User of preg_split()

This function can be used to break a string into multiple ones based on the regular expression. this could be helpful after we have a string, let’s say date string separated by -. we are able to break that string with – key.

The following above of the code segment will break the string either by – or by the white space.

2. User of explode() Function

This function can be used to break a string into an array of strings.
Let’s understand the same with a simple example:

3. The user of str_split

This can also do the Identical job, as converting a string to an array of smaller strings.

PHP Split String Example

Example 1: Split a string where we find the white space.

Actual String: Welcome to United States.
Array of string after string split:
Array (
[0] => Welcome
[1] => to
[2] => United
[3] => States.
Example 2: string can be split into multiple strings using a regular expression

Actual String: 2019-04-17 11:25:25
Array of string after string preg_split:
Array (
[0] => 2019
[1] => 04
[2] => 17
[3] => 11
[4] => 25
[5] => 25 )
Example 3: string split using the str_split() function.

Actual String: Welcome to United States
Array of string after string str_split:
Array (
[0] => W
[1] => e
[2] => l
[3] => c
[4] => o
[5] => m
[6] => e
[7] =>
[8] => t
[9] => o
[10] =>
[11] => U
[12] => n
[13] => i
[14] => t
[15] => e
[16] => d
[17] =>
[18] => S
[19] => t
[20] => a
[21] => t
[22] => e
[23] => s )
Example 4: String split using str_split() function with a specified length.

Actual String: Welcome to United States
Array of string after string str_split:
Array (
[0] => Welc
[1] => ome
[2] => to U
[3] => nite
[4] => d St
[5] => ates )

Explanation: we will say that some string of length two or three however we’ve mentioned the length as a four. this can be simply because the string element with 2 carries 2 spaces and the string with 3 characters carry one space with it.

Example 5: To get the first string from of that string.

Actual String: What is your name?
Array of string after string str_split:
Array (
[0] => What
[1] => is
[2] => your
[3] => name? )
Now getting the first element from this array: What


In this article, you have learned to PHP split string function and there are various ways to handle the string split. Built-in PHP functions can be used as per the needed to process the split string. I hope you will enjoy it!. if have any query related program then contact me at I will try to resolve the problem. Stay healthy, stay safe! Happy coding! enjoy it!