TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-25-2008, 10:43 PM   #1 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Default 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!
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 11-25-2008, 11:51 PM   #2 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Default

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?
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 11-26-2008, 12:20 AM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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

Correct?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-26-2008, 05:57 AM   #4 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

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.
tony is offline  
Reply With Quote
Old 11-26-2008, 12:47 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I know you should do that, but is it mandatory?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-26-2008, 01:22 PM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
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.
Salathe is offline  
Reply With Quote
Old 11-26-2008, 03:41 PM   #7 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Default

Quote:
Originally Posted by tony View Post
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
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 11-26-2008, 04:51 PM   #8 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

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
tony is offline  
Reply With Quote
The Following User Says Thank You to tony For This Useful Post:
buildakicker (11-26-2008)
Old 11-26-2008, 05:17 PM   #9 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Default

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!
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 11-26-2008, 06:43 PM   #10 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

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



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:23 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design