PDA

Click to See Complete Forum and Search --> : Parse txt file, line by line..


Jmacp
Apr 18th, 2007, 02:52 AM
I have a txt file thats storing data, so each new line will look like,

aaa||bbb||ccc
sss||ttt||rrr


etc..

any idea the best way to split each line one by one on the "||" then display those entries row by row, something like,

guestbook table,

name email comment
aaa bbb ccc
sss ttt rrr

Have been trying for ages, playing about with,

$file = fopen("guestbook.txt", "r");
while(!feof($file))
{
$wr= fgets($file);
$s=explode("||",$wr);
for($i=0;$i<sizeof($s);$i++){

.....

kinda things....

penagate
Apr 18th, 2007, 03:04 AM
This will put the data into a jagged array:
$guestbook_lines = file('guestbook.txt');
$guestbook = array();

foreach ($guestbook_lines as $n => $line)
$guestbook[$n] = explode('||', $line);

Jmacp
Apr 18th, 2007, 02:04 PM
I have this in my txt file,

test1||test2||test3
test1||test2||test3
test1||test2||test3

So this put each line into an array,

$guestbook_lines = file('guestbook.txt');


you can then split each line on the || which all looks fine,
$guestbook = array();
foreach ($guestbook_lines as $n => $line){
$guestbook[$n] = explode('||', $line);

}

but if i do this the return output is ArrayArrayArray

for($i=0;$i<sizeof($guestbook);$i++){
echo $guestbook[$i];
}

penagate
Apr 18th, 2007, 02:08 PM
Yes. If you want to recursively inspect the structure of a variable, use print_r or var_dump.

var_dump($guestbook);


That will show you that it is a jagged array (as I said).