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);
?>

Answers


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);

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; }
    }
}

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.


Need Your Help

Removing range (tail) from a List

java list collections stack

Is there an efficient method to remove a range - say the tail - of X elements from a List, e.g. LinkedList in Java?

Why use ASP.NET MVC partial View (.ascx)

asp.net-mvc

As the topic says. What are the reasons and scenarios why I am adding a view for a controller method, that I should select the checkbox "Create a partial view (.ascx)?