TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Reading from txt file.. (http://www.talkphp.com/general/1229-reading-txt-file.html)

marxx 09-25-2007 07:57 PM

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

Wildhoney 09-25-2007 08:11 PM

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 />';



marxx 09-25-2007 08:20 PM

wow.. Thanks for faaaast reply!

Well, yeah.. I think I can go from here. It looks actually really simple..
Thank you Wildhoney.

Tanax 09-25-2007 08:29 PM

Haha, that looked really simple wild~ :D


All times are GMT. The time now is 07:16 PM.

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