home about us contact us

free scripts advanced scripts online tools great books web related tutorials contributed tutorials news archive geek toys!

help forum live chat help

Splitting a String of Data with PHP.

The basic data string.

Often you get a string of data in a PHP script you are working on that has several sub-parts. For example, you may have a string of data like this: identification number|name|address|phone number|email address.

What is significant about this is that it contains sub-sets of data, often called fields in a row of data. Each field in this example is separated by a special character called a pipe (usually created with the shifted backslash key on a keyboard). If I split or divide the data at each pipe, then I would get this:

You may see other types of delimiters in common usage. A comma delimited or Comma Separated Value (CSV) file for example might look like this:
"identification number","name","address","phone number","email address".
It may or may not have quotes around each data item. Other common delimiters are the tab character, the space character, and occaisionally the slash or backslash. (Tip for beginners: use the pipe character as it is less likely to occur in typical text.)

It doesn't have to be on delimiters either however. You can break a string on any character. For example the sting "this is a fun example of this" could be split at the character "i" resulting in this:

Notice that in the resulting data sub-parts, you get lose the delimiting character.

This tutorial is all about how to take a string like this and break it into the sub-parts. The sub-parts are rendered as an array that can then be addressed or manipulated (see for example our tutorial: PHP Reference - array functions list. Of course as we show you how to bust them out, we need to also show you how to recombine them at the end of the tutorial. There are several ways to achieve the data split: explode(), preg_split(), spliti(), and split(). Split strings can be recombined using implode().


Like Harry Potter waiving his wand, you too can do amazing things with your PHP wand. If you use the function explode you can divide your data into an array of sub-parts or fields. It uses this syntax: array explode ( string separator, string [, integer limit]). So replace "array" with your string name, replace "string separator" with your delimiting character, replace "string" with the string you wish to divide (which can be the actual string or a variable which contains the string). Optionally you can specify a maximum number of array elements. If you specify a lower number of elements than you would otherwise get, all remaining content of the string is dumped into the last element without being divided. Usually some examples are instructive.

You can divide our first sting above using this code:

$data = 'identification number|name|address|phone number|email address';
$splitdata = explode('|', $data);
echo "$splitdata[0], $splitdata[1], $splitdata[2], $splitdata[3], $splitdata[4]";

This should output the following:

    identification number, name, address, phone number, email address

(Hint: to avoid feeling like an idiot -- well at least I feel like one after I spend hours trying to figure out where I went wrong only to find out this is the problem, remember always that the first element in an array is "zero indexed" so that a five element array has numbers 0, 1, 2, 3, and 4. Not 1, 2, 3, 4, and 5.)

Note that we strongly prefer using single quotes in our work whenever possible, but we need to use double quotes in the above example for the echo command. So if you try to duplicate these examples, pay extra attention to the type of quotes used. Hopefully at this time you are completely with me. If not, go back and re-read this part as it is very important that you have an understanding to fully appreciate the rest of the tutorial and we won't explain as well with the other functions as they are largely repetitive.

Currently being finished ... email us if you need it before I get it done ;-)