Page 1 of 1

Splitting a string line by line

Posted: Fri Mar 19, 2004 2:20 am
by Jim
Using explode, I'd like to split something like the following:

Code: Select all

This|is|a|line
This|is|a|line2|
Where each line is separated in to elements [0] , [1] , [2] and [3].
The question I have is how to reset the pointer at the beginning of each new line to ensure I don't have [0] [1] [2] [3] [4] [5] [6]... etc.

What thinkest thou?

Posted: Fri Mar 19, 2004 2:25 am
by markl999
You mean so you end up with a multidimension array, where [0][2] is the third word of line 1, and [1][1] is the second word of line 2 etc ?
If so then something like the following should do it.

Code: Select all

<?php
$lines = file('line.txt'); //file containing the text lines
foreach($lines as $key=>$val){
  $lines[$key] = explode('|', $val);
}
echo $lines[0][3]; //shows line
echo $lines[1][1]; //shows is
?>

Posted: Fri Mar 19, 2004 3:13 am
by Jim
Excellent dude.

Next question - how do I ensure results are put on the next line at every submission?

Thanks man.

Posted: Fri Mar 19, 2004 3:18 am
by Jim
Never mind last q :)