1

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);
?>
Sweepster
  • 1,829
  • 4
  • 27
  • 66
  • In what time must the time counter start? – elxordi Apr 12 '12 at 19:48
  • this screams out -use a data base, not a flat file. –  Apr 12 '12 at 19:48
  • I can't use a database, I must parse the file – Sweepster Apr 12 '12 at 19:51
  • @elxordi The time it starts at is to be determined by the variables set at the beginning of the code. – Sweepster Apr 12 '12 at 19:53
  • @Dagon: A file is a database. – hakre Apr 12 '12 at 19:56
  • Out of curiosity, why are you incrementing the time manually, rather than checking the actual time elapsed? I would expect your writes to be faster than (or slower than) exactly one per second, so the "timestamps" will not actually correspond to reality. – horatio Apr 12 '12 at 20:34
  • It is just an example for a script I'm writing in which each line in a TXT file will be printed at a given time in the future (not necessarily by the second), that time being determined by the loop. Now that I now how to do it, I can increment my time by any amount of seconds I need – Sweepster Apr 12 '12 at 21:12

3 Answers3

1

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);
elxordi
  • 476
  • 3
  • 11
0

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; }
    }
}
Leven
  • 530
  • 3
  • 10
0

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.

hakre
  • 193,403
  • 52
  • 435
  • 836