PHP String Split: How and Why Is It Used? Illustrated With a Detailed and Easy to Understand Example

PHP String Split: How and Why Is It Used? Illustrated With a Detailed and Easy to Understand Example
Page content

Why and Where is PHP String Split Used?

When data is stored in a database or when a MS Access file is exported, an option to export the data in CSV is given to the user. The data is a set of comma separated values. For instance the data could be exported as follows:

Joe,14,manager,55,California,USA.

In cases like these, developers would split the string so that parts of the string can be accessed individually. Using PHP string split, a string can be stored in an array using simple functions provided by PHP and they can be operated upon. Other common cases where strings are split are date fields, so that the date, month and the year can be stored in an array, formatted as desired and displayed or manipulated for other operations.

PHP String Split: How to Use it?

Splitting strings in PHP can be done in two ways by using either the split() function or the explode() function. The PHP string split function (which is the split() function ) is deprecated, meaning, the php split() function will not be supported in the future versions of PHP. The current PHP version is PHP 5.3.0 and it is recommended that the PHP string split function be used by using the PHP explode() function which returns the same results.

Syntax of PHP explode():

explode(delimiter, string_to_be_split)

PHP 4.0.1 + versions include an extra argument called limit which is of the type int.

explode(delimiter, string_to_be_split, limit)

The explode function takes three arguments:

  1. delimiter - The delimiter should be a string. It is the string that is used as a parameter based on which strings are split and stored into arrays.
  1. string_to_be_split - Enter the string to be split. It is a good practice to store this string in a variable and then use the variable in this part of the arguments list.

  2. limit - An integer value that specifies the number of array elements into which the string has to be split based on the delimiter. This is an optional value and can be left out.

Working Examples

In this example, I have used a string that is delimited by commas. The string is stored in a variable. The print_r function is used to display the output on a web page. The first argument of the explode function takes a comma which is the delimiter in this string. The second argument takes the variable in which the string has been stored.

When the PHP page is viewed using a web browser, the following output is displayed.

Array ( [0] => one [1] => two [2] => three [3] => four [4] => five [5] => six [6] => seven [7] => eight [8] => nine [9] => ten )

Thus the strings can be split and stored in an array and accessed individually.

In this second example, I have used the explode function that takes three arguments. The only difference between the above example and the following example is that, you can restrict the number of elements of the array in which the string that has been split is stored.

Output:

Array ( [0] => This [1] => is [2] => a–bad–string )

The code is pretty self explanatory. I have used 3 in the place where the limit argument has to be entered. So there are three elements present in the array. The first two contain the string that has been split, the last one contains the rest of the string along with the delimiters.