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