TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Read Text File in Reverse (http://www.talkphp.com/general/3653-read-text-file-reverse.html)

buildakicker 11-25-2008 10:43 PM

Read Text File in Reverse
 
Hello all,

I have a text file that contains enteries like so:

Code:

date||name||title||description
I would like to read my file in reverse, ie from the Bottom Up.

I can read it backwards using:

Code:

$file = array_reverse( file( 'u4g.txt' ) );
foreach ( $file as $line ){

ect....

but cannot figure out how EXPLODE each line in a foreach. Why can't I do something like this:

Code:

$file = array_reverse( file( 'u4g.txt' ) );
foreach ( $file as $line ){
        $parts = explode('||', $line);
        echo "<div class='viewline'><h3>" . $parts[3] . "</h3><br />". $parts[4] ."<br />Posted by: " . $parts[1] . "<br /> Date Posted: " . $parts[0] . "<br />" . "Contact Email: " . $parts[2] . "</span></div>";
        }
}

Do I need something like:

Code:

$line_of_text = fgets($fp);
In there???

Thanks!

buildakicker 11-25-2008 11:51 PM

So I used this:

Quote:

$parts = array_reverse(explode('||', $line_of_text));
and it puts all the pieces in the file in $parts[] backwards... but I need the entire lines. So maybe array reverse isn't right?

Wildhoney 11-26-2008 12:20 AM

Quote:

date||name||title||description
date2||name2||title2||description2
Output:
description2, title2, name2, date2
description, title, name, date

Correct?

tony 11-26-2008 05:57 AM

you need to declare $parts as an array first before assigning an array value to it. like this:
PHP Code:

$file array_reversefile'u4g.txt' ) );
$parts=array();
foreach ( 
$file as $line ){
    
$parts explode('||'$line);
    echo 
"<div class='viewline'><h3>" $parts[3] . "</h3><br />"$parts[4] ."<br />Posted by: " $parts[1] . "<br /> Date Posted: " $parts[0] . "<br />" "Contact Email: " $parts[2] . "</span></div>";
    }


that's is a problem i stumble sometimes with arrays.

Wildhoney 11-26-2008 12:47 PM

I know you should do that, but is it mandatory?

Salathe 11-26-2008 01:22 PM

Quote:

Originally Posted by Wildhoney (Post 19924)
I know you should do that, but is it mandatory?

In short, no. ;-) You're just assigning a new value to $parts from the call to explode just like giving it a fixed value (like 1.3 or 'fixed value'). There's no need to create the variable beforehand.

buildakicker 11-26-2008 03:41 PM

Quote:

Originally Posted by tony (Post 19922)
you need to declare $parts as an array first before assigning an array value to it. like this:
PHP Code:

$file array_reversefile'u4g.txt' ) );
$parts=array();
foreach ( 
$file as $line ){
    
$parts explode('||'$line);
    echo 
"<div class='viewline'><h3>" $parts[3] . "</h3><br />"$parts[4] ."<br />Posted by: " $parts[1] . "<br /> Date Posted: " $parts[0] . "<br />" "Contact Email: " $parts[2] . "</span></div>";
    }


that's is a problem i stumble sometimes with arrays.

Thanks for the reply. I have tried that, but I don't get anything to show up in the output for some reason. I was initally using foreach(). When you use array_reverse(file('')); does that read the text file lines from the bottom up?

I don't want to read each line in reverse, I want to read the entire text file lines in reverse, but keep the lines data in the correct order.

ie take this in the text file:

Code:

data||email||name||description
data2||email2||name2||description2

and output it like this:
Code:

data2||email2||name2||description2
data||email||name||description

Thanks ^^

tony 11-26-2008 04:51 PM

I ran the code:

PHP Code:

<?php
$file 
array_reversefile'u4g.txt' ) );
foreach ( 
$file as $line ){
    
$parts explode('||'$line);
    echo 
$parts[0].'||'.$parts[1].'||'.$parts[2].'||'.$parts[3].'<br />';
}
?>

with this dummy text file:

Code:

data1||email1||name1||description1
data2||email2||name2||description2
data3||email3||name3||description3
data4||email4||name4||description4
data5||email5||name5||description5
data6||email6||name6||description6

and my output was this:

Code:

data6||email6||name6||description6
data5||email5||name5||description5
data4||email4||name4||description4
data3||email3||name3||description3
data2||email2||name2||description2
data1||email1||name1||description1

are you sure there is not something else that is conflicting with the code? maybe the variable $path is used somewhere else, or your file function is not supported in your php installation (I don't think that might be the case, but still), or maybe your file is protected?

It's been a while since I worked with files

buildakicker 11-26-2008 05:17 PM

Thanks Tony,

I am not sure why the first code snippet wouldn't work, but that one did.

It runs it correctly too.

So just starting with:
Code:

$file = array_reverse( file( 'u4g.txt' ) );
is the way to read the file in reverse. ^^

GREAT!

Thanks a bunch!

tony 11-26-2008 06:43 PM

No problem, it's weird that it didn't work since is the same procedures. But it worked, that is always a relief.


All times are GMT. The time now is 06:29 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0