Sweepster
Sweepster

Reputation: 1949

PHP for each line in TXT, increase value of variable?

I have the following code with reads a TXT file, pulls out unneeded info from each line, then stores the edited lines in a new TXT file.

<?php
$file_handle = fopen("old.txt", "rb");
ob_start();

while (!feof($file_handle) ) {

$line_of_text = fgets($file_handle);
$parts = explode('\n', $line_of_text);

foreach ($parts as $str) {
 $str_parts = explode('_', $str); // Split string by _ into an array
 array_pop($str_parts); // Remove last element
 array_shift($str_parts); // Remove first element
 echo implode('_', $str_parts)."\n"; // Put it back together    (and echo newline)
}
}

$new_content = ob_get_clean();
file_put_contents("new.txt", $new_content);

fclose($file_handle);
?>

I now want to insert $hr #min and $sec variables that will increase by 1 second every time a new line is saved. Let's say my lines read like this (old code):

958588
978567
986766

I want my new code to look like this:

125959958588
130000978567
130001986766

As you can see, the hour is in 24hr format (00 - 23), followed by minutes (00 - 59), and seconds (00 - 59) with the extracted txt at the end.

I've laid down the variable framework but I don't know how to get the vriables to increment properly. Can someone help?

<?php
$file_handle = fopen("old.txt", "rb");
$hr = 00;
$min = 00;
$sec = 00;
ob_start();

while (!feof($file_handle) ) {

$line_of_text = fgets($file_handle);
$parts = explode('\n', $line_of_text);

foreach ($parts as $str) {
 $str_parts = explode('_', $str); // Split string by _ into an array
 array_pop($str_parts); // Remove last element
 array_shift($str_parts); // Remove first element
 echo $hr.$min.$sec.implode('_', $str_parts)."\n"; // Put it back together  (and echo newline)
}
}

$new_content = ob_get_clean();
file_put_contents("new.txt", $new_content);

fclose($file_handle);
?>

Upvotes: 1

Views: 1427

Answers (3)

elxordi
elxordi

Reputation: 486

I would go much easier:

<?php
$contents = file('old.txt');
$time = strtotime('2012-01-01 00:00:00'); // Replace the time with the start time, the date doesn't matter
ob_start();

foreach ($contents as $line) {
    $str_parts = explode('_', $line); // Split string by _ into an array
    array_pop($str_parts); // Remove last element
    array_shift($str_parts); // Remove first element

    echo date('His', $time) . implode('_', $str_parts) . "\n"; // Put it back together  (and echo newline)

    $time += 1;
}

$new_content = ob_get_clean();
file_put_contents("new.txt", $new_content);

Upvotes: 1

hakre
hakre

Reputation: 197787

The format you have is of a date in the UNIX domain, for example the first date:

gmdate('His', 0);    # 000000
gmdate('His', 60);   # 000100
gmdate('His', 3600); # 010000

So you can just pass in the number of seconds and the gmdate function will format it for you.

Upvotes: 0

Leven
Leven

Reputation: 530

I think you're looking for something like this in the inner loop:

$sec++;
if (($sec==60) { 
    $min++; 
    $sec=0 
    if (($min==60) { 
        $hr++; 
        $min=0; 
        if (($hr==25) { $hr=0; }
    }
}

Upvotes: 0

Related Questions