Convert Strings to Arrays in PHP

PHP provides functions that convert strings to arrays. The explode function splits the string where it finds the delimeter you specify. The preg_split function uses a regular expression to specify the delimiter and provides options to control the resulting array. The str_split function splits a string into array elements of equal length. Also, a string can be treated as an array of characters to some extent, as we discuss below.

We will begin our discussion with the explode function.


Pass a delimiter and a string to the explode function, and it will split the string into array elements where it finds the delimiter. The delimiter can be a single character (such as a space or a comma), or it can consist of multiple characters.

In the following example, a string contains a list of items separated by a comma and a space. We use the explode function to convert this list to an array by passing a delimiter string consisting of a comma and a space (', ') as the first argument. We pass the string we want to convert as the second argument:

// string to convert
$fruits = 'apple, orange, pear, banana, raspberry, peach';
// pass delimiter and string to explode function
$fruits_ar = explode(', ', $fruits);
// view result using var_dump
/* View Source display:
array(6) {
  string(5) "apple"
  string(6) "orange"
  string(4) "pear"
  string(6) "banana"
  string(9) "raspberry"
  string(5) "peach"

In our next example we use a forward slash (/) as a delimiter to split a path name into an array of directories:

$path = '/home/someuser/Documents/notes/misc/';
// split $path on '/'
$dirs = explode('/', $path);
/* View Source display:
array(7) {
  string(0) ""
  string(4) "home"
  string(8) "someuser"
  string(9) "Documents"
  string(5) "notes"
  string(4) "misc"
  string(0) ""

Notice in the result that the first element and last element in the array contain empty strings. This is because nothing precedes the first forward slash or follows the last forward slash. Nonetheless, the original string is split at these points creating array elements.

If the delimiter string is not found within the string you are converting, an array of one element will be returned, and that element will contain the entire string. The explode function provides an optional limit parameter, in case you would like to control the number of array elements returned. Find out more in the PHP Manual.

The preg_split function is similar to explode except that it uses a regular expression to specify the delimiter. It also provides options that give more control over the array returned.


The str_split function converts its string argument to an array with elements of equal length. You can pass a length as the second argument, or it will default to 1. In our example, we pass 3 to create an array whose elements have three characters each:

$str = 'abcdefghijklmnopqrstuvwxyz';
// pass 3 to split $str into an array with elements 3 characters long
$split = str_split($str, 3);
// display result using print_r
/* View Source display:
    [0] => abc
    [1] => def
    [2] => ghi
    [3] => jkl
    [4] => mno
    [5] => pqr
    [6] => stu
    [7] => vwx
    [8] => yz

The last entry in the array contains the remaining characters, even if fewer than the length argument specifies.


The str_word_count function converts a string to an array of words when passed a second argument.

Strings as Arrays of Characters

Strings aren't really arrays, but you can access the characters in a string using array syntax, as the following demonstrates:

$str = 'top dog';
// display 3rd character in $str
echo $str[2]; // p

// set 3rd character to new value
$str[2] = 'y';
echo $str; // toy dog

First we use square brackets to access the third character in the string. Then we set it to a new value and display the results using echo.

You can access individual characters in a string using a for loop. We demonstrate by using a for loop to see how many times the letter 'a' (upper or lowercase) occurs in the example string:

$str = 'An example string';
$count = 0;
for ($i=0, $len=strlen($str); $i<$len; $i++ ) {
    if ( strpos('Aa', $str[$i]) !== false ) {

echo $count; // 2

In the for loop, we inspect each character in turn, using the strpos function to check if it is in 'aA'. If it is, we increment the $count variable. We display the result using echo once outside the for loop.

Find out more about strpos and other search functions.

Although a string can be treated as an array of characters to some degree, you can't unset individual characters, or apply array functions to a string.

Back to top