PHP: How to Convert String to an Array [explode()|str_split]

By Parth Patel on Nov 02, 2020

PHP provides various functions to convert a String to an Array. In this article, we will discuss 3 ways to convert string to array in PHP using 3 different methods:

  1. explode()
  2. str_split()
  3. str_word_count()

1) Convert String to Array using explode()

explode() method is one of the built-in function in PHP which can be used to convert string to array.

The explode() function splits a string based on the given delimiter. A delimiter acts as a separater and the method splits the string where the delimiter exists.

Example: Convert comma separated string to array in PHP

$string = "one,two,three,four";
$arr = explode(",", $string);

Array ( [0] => one [1] => two [2] => three [3] => four )

In above example, the string contains four words separated by a comma. Normally, in csv files, each column values in a row is separated by comma.

So, to extract each value and store it in array, we are converting string to array using explode() function. The returned array will contain list of words as elements.

Convert string into an array using limit

The explode() also accepts optional 3rd parameter to limit the number of splits. By default, there is no limit so it splits at all occurances of the separater. But if limit is given, then it will split only to maximum $limit parts.

Let's review an example:

$string = "My name is PHP and I am the best";
$arr = explode(" ", $string, 2);
// Array ( [0] => My [1] => name is PHP and I am the best )

You can see in above example that if $limit is set to 2 and it will split only once thereby splitting whole string into two parts.

Also Read: How to convert array to string in php

2) Convert String to Array using str_split()

The str_split() is another standard built-in method provided by PHP to split strings into an array easily. This function converts the given string into equal parts of provided length.

It differs from previously explained explode() function as it doesn't use any kind of separator. It simply cuts the string into equal parts, whereas explode() method splits strings into multiple parts not necessary of equal length but separated by given separator.

It accepts string as first argument which is required. You can pass the length as second parameter which is optional. If no length is passed then it will split string into parts with length = 1.

Let's try an example:

$string = 'Hello World';

$arr = str_split($string, 3);


//Array ( [0] => Hel [1] => lo [2] => Wor [3] => ld )

Note: Last element doesn't necessorily have to be of the given length.

Also Read: Push to Array in PHP

3) Convert String to Array using str_word_count()

The str_word_count() method is not normally used to split string to an array. It's primary use is to provide information about the words used in a string which can be used to actually split strings into words separated by space if optional second argument for format is passed.

Remember, you can only split string into words. You can provide $charlist as third optional argument to allow additional characters to be considered as 'word'.

Let's review an example:

$string = "Hello world, you're are welcome!";

//Now, if you don't provide second argument, it will simply return number of words in given string
// 5

// Array ( [0] => Hello [1] => world [2] => you're [3] => are [4] => welcome )

Pretty smart, eh!

So, these are different ways you can split string into array using PHP. Hope, this article helped you.


Also Read: How to generate unique ID in php