Splitting a line of text in PHP


PHP allows you to split a line of text into its component parts. For example, if you were reading from a text file line by line you might have to break apart a line like this:

Poll number 1, 1500, 250, 150, 100, 1000

If this were a poll, and you want to display the results for all to see, then you might be trying to print something like this on the page:

Poll Number 1
Respondents: 1500
Answer A: 250
Answer B: 150
Answer C: 100
Answer D: 1000

The line of text is separated by commas. As the line is read in (which we'll see how to do in a later section), you'd be passing it to a variable. You'd then need to chop the text up, based on the comma. We can simulate that. First, pass the text to a variable:

$text_line = "Poll number 1, 1500, 250, 150, 100, 1000";

The next job is to split this text apart, so that PHP knows about all the separate pieces. The pieces we want are:

Poll number 1

To split lines of text, the gloriously sounding explode( ) function can be used. You just provided it with the text you want to split, and the character that is used to separate each piece. Here's the syntax:

explode( separator, string_to_split )

In between the round brackets of explode( ) the separator you want to use goes first, followed by a comma, then the string you want to split. For our line of code above, you'd do this:

$text_line = "Poll number 1, 1500, 250, 150, 100, 1000";
$text_line = explode( "," , $text_line );

So we're saying, "Look for a comma in the text, and split the line of text into separate pieces." Once PHP does its job, it puts all the parts into the variable on the left hand side of the equals sign ( = ), which was $text_line for us. This variable will then be an array!

To get at the pieces of the array, access it in the normal manner. Here's some code to try:


$text_line = "Poll number 1, 1500, 250, 150, 100, 1000";
$text_line = explode(",",$text_line);

print $text_line[0];


Run the code and see what happens. Then change the 0 of the print statement to 1, then to 2, then to 3, then to 4, then to 5, and finally to 5. What happens when you enter 6 as the array Key number?

To see all the parts of your array, you can use a different form of print statement. Try changing the print line in your code from this:

print $text_line[0];

to this:


Run your code and see what happens.

You should see your array details printed out, with all the Keys and the Values. The print_r( ) statement is quite useful, when you're trying to debug your code.

And it does show that explode( ) works – all of the values are in an array!

Another way to access all the element returned by explode( ) is with a for loop:

$text_line = "Poll number 1, 1500, 250, 150, 100, 1000";
$text_line = explode(",",$text_line);

for ($start=0; $start < count($text_line); $start++) {

print $text_line[$start] . "<BR>";


In the for loop above, we set a start value to zero. The end condition is this:

$start < count($text_line)

We use the count( ) function to get the number of elements in the array called $text_line. Each time round the loop, PHP checks to see if the value in the variable called $start is less than how many elements are in the array. It breaks out of the loop when $start is NOT less than count($text_line).

Inside the loop, we have a normal print statement:

print $text_line[$start] . "<BR>";

To get at each element in the array, this is used:


The variable called $start will be different each time round the loop. So the value at each position is printed. The "<BR>" at the end just adds a HTML line break.


As well as exploding a line of text, you'll need to know how to join the parts back together again. We'll do that in the next part.