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 09-25-2007, 07:57 PM   #1 (permalink)
The Contributor
 
marxx's Avatar
 
Join Date: Sep 2007
Location: Finland
Posts: 45
Thanks: 3
marxx is on a distinguished road
Default Reading from txt file..

Hello.. This is my first question and just want to say before it that this is awesome forum!! Thanks for that..

Ok.. I need to read text file and every second line ONLY!?
Like this:
0
2
4
6
8
10
and so on..

I have here my script which will print all lines. If someone could look at it and maybe quide me how to modify this so it will print only every second line like I sayd before.
Code:
$filename = "postitoimi.txt";
$file = fopen($filename,"r");

$print = file("postitoimi.txt");
$lines = count($print);

$lines2 = $lines - 1;
$i = 0;
while ($i <= $lines2)
{
$patterns = array("(","'",")",",");
$replacements = array("","",""," ");
$data = str_replace($patterns, $replacements, $print[$i]);

$data = explode(",", $data);
print($data[0] . $data[1] . "<br />");

$i++;
}
Reason why I need to do this is that, in that textfile there is 2 information for every one ID number. And those are all inviditual lines. My customers old CMS is doing that now I need to convert that into my own CMS..

anyway, thanks for all help!


- Marxx
Send a message via MSN to marxx
marxx is offline  
Reply With Quote
Old 09-25-2007, 08:11 PM   #2 (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

This code gives you a basic idea of how to do this. We loop through the lines in the file and issue the continue construct to any lines that are odd. So it begins at line 1, skips line 2, prints line 3, etcetera...

Glad you like it here as well :) !

PHP Code:
$aContents file('file.txt');

for(
$iIndex 0$iIndex count($aContents); $iIndex++)
{
    if(
$iIndex == 1)
    {
        continue;
    }
    
    echo 
$aContents[$iIndex] . '<br />';

__________________
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 09-25-2007, 08:20 PM   #3 (permalink)
The Contributor
 
marxx's Avatar
 
Join Date: Sep 2007
Location: Finland
Posts: 45
Thanks: 3
marxx is on a distinguished road
Default

wow.. Thanks for faaaast reply!

Well, yeah.. I think I can go from here. It looks actually really simple..
Thank you Wildhoney.
Send a message via MSN to marxx
marxx is offline  
Reply With Quote
Old 09-25-2007, 08:29 PM   #4 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Haha, that looked really simple wild~ :D
Tanax 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 07:55 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