PHP Explode Function (PHP String to Array)

The PHP explode function allows you to break a string into smaller pieces with each break occurring at the same symbol. This symbol is known as the delimiter. For example, you can easily break a sentence into words by using the space between words as the marker which tells PHP to break there.

Once the string is broken up, the pieces are stored in an array for easy manipulation by the programmer. The following tutorial on the PHP explode function will show you how it's done.

PHP Explode Syntax

Below is the syntax for the explode function.

$array_name = explode($delimiter, $string);

The variable $array_name will contain an array of the pieces of the string. The $delimiter variable holds the character which PHP should break at, and $string is obviously the string that we are working on.

PHP Explode Example

Take a look at the following example of using the explode function in PHP:

$sentence = 'I love Tutorial Arena so much';
// break $sentence using the space character as the delimiter
$words = explode(' ', $sentence);
//tell the amount of words using the size of the array
echo 'The sentence has ' . count($words) . ' words.<br />';
// loop through and print all the words
for ($i = 0; $i < count($words); $i++)
    echo 'Word ' . $i . ' - ' . $words[$i] . '<br />';

The result would be:

The sentence has 6 words.
Word 0 - I
Word 1 - love
Word 2 - Tutorial
Word 3 - Arena
Word 4 - so
Word 5 - much

As we can see above, breaking a sentence into words is very easy when using the PHP explode function. The function analysed the sentence and everytime it found a space (which was the delimiter we chose) is broke that part of the sentence off and placed it into the result array. This effectively broke the sentence into its constituent words.

PHP Explode Function Example with a Limit

If we wanted to limit the number of pieces (size of the array) that is returned by the explode function, we would have to pass an addition parameter to the function which specifies the size limit.

When we pass a limit to it, the PHP explode function will work just the same as before until the array reaches the maximum size specified by the limit. At this point, the function will just return the remainder of the string ignoring any further breaks that may have been possible. Look at the following example:

$sentence = 'I love Tutorial Arena so much';
// the size of $pieces should not be more than 3
$pieces = explode(' ', $sentence, 3);
echo 'The sentence has ' . count($pieces) . ' pieces.<br />';
for ($i = 0; $i < count($pieces); $i++)
    echo 'Piece ' . $i . ' - ' . $pieces[$i] . '<br />';

The result would be:

The sentence has 3 pieces.
Piece 0 - I
Piece 1 - love
Piece 2 - Tutorial Arena so much

As we can see, the resulting array is limited in size to the value of the third parameter which is passed to the function.

PHP Explode Function - Parsing a CSV file

As a reminder, a CSV (comma separated value) file is one in which data is stored using commas to separate data which is in a row. For example, a CSV file may look like:


We see that each set of data is on a new line and that the data on each line is separated using commas. It should be clear that we can use the PHP explode function to break at each newline to separate the rows of data, and then use another explode function to separate the pieces of data in each row.

If we had the data above in a file called data.csv, we could retrieve it as follows:

// get data from the file
$data = file_get_contents('data.csv');
// use the newline as a delimiter to get different rows
$rows = explode("\n", $data);
// go through all the rows starting at the second row
// remember that the first row contains the headings
for($i = 1; $i < count($rows); $i++)
	$temp = explode(',', $rows[$i]);
	$type = $temp[0];
	$name = $temp[1];
	$size = $temp[2];
	echo "The $name is a $size $type. <br />";

As you can see in the code, we used the PHP explode function twice; first to separate the lines, and then to separate the data on each line. The result of the above code would be:

The cat is a small animal.
The whale is a large animal.
The spider is a small insect.

We hope that this tutorial on the PHP explode function has been helpful. As you can see, it is a very useful function with many applications.

You may also want to have a look at the PHP Implode Function which does the opposite of this PHP Explode Function.

Link to this Page

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...