Ole Sørensen
Ole Sørensen

Reputation: 349

Read every second line from txt file with php

I have this test here: http://flamencopeko.net/news2 working.

But I want the dates to be in their own tr's. So if I put dates on their own lines in the text file, I'd love a suggestion on how to read every second line from txt file with php. They would probably become two arrays then. One for date and one for news-content.

Source: http://flamencopeko.net/news2/index.txt

Code:

<?php
$file = fopen("news_2013.txt", "r");

$i = 0;
while (!feof($file)) {
    $posts[] = fgets($file);
}
fclose($file);

foreach ($posts as $x){
    echo '<table><tr><td align="justify">'.$x.'</td></tr></table><br/>';
}
?>

It should look like this: http://flamencopeko.net/news. That page have the html table etc. tags in the text file. We don't want that.

Upvotes: 2

Views: 1492

Answers (1)

SteAp
SteAp

Reputation: 11999

As far as I see, you don't want to read every second line, but to identify different parts of one input line.

How about this one:

foreach ($posts as $rawPost ){

    $datePart = substr( $rawPost, 0, 19 );
    $newsPart = substr( $rawPost, 19, 10000 );

    echo '<table><tr>'
       . '<td>' . $datePart . '</td>'
       . '<td align="justify">' . $newsPart . '</td>'
       . '</tr></table><br/>'
       ;

}

Upvotes: 2

Related Questions