|
-
Apr 18th, 2007, 02:52 AM
#1
Thread Starter
Frenzied Member
Parse txt file, line by line..
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,
PHP Code:
$file = fopen("guestbook.txt", "r");
while(!feof($file))
{
$wr= fgets($file);
$s=explode("||",$wr);
for($i=0;$i<sizeof($s);$i++){
.....
kinda things....
-
Apr 18th, 2007, 03:04 AM
#2
Re: Parse txt file, line by line..
This will put the data into a jagged array:
PHP Code:
$guestbook_lines = file('guestbook.txt');
$guestbook = array();
foreach ($guestbook_lines as $n => $line)
$guestbook[$n] = explode('||', $line);
-
Apr 18th, 2007, 02:04 PM
#3
Thread Starter
Frenzied Member
Re: Parse txt file, line by line..
I have this in my txt file,
test1||test2||test3
test1||test2||test3
test1||test2||test3
So this put each line into an array,
HTML Code:
$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];
}
-
Apr 18th, 2007, 02:08 PM
#4
Re: Parse txt file, line by line..
Yes. If you want to recursively inspect the structure of a variable, use print_r or var_dump.
PHP Code:
var_dump($guestbook);
That will show you that it is a jagged array (as I said).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|