Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7776993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:01:21+00:00 2026-06-01T18:01:21+00:00

I have the following code with reads a TXT file, pulls out unneeded info

  • 0

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);
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:01:23+00:00Added an answer on June 1, 2026 at 6:01 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that reads mac addresses from a file and tries
When I using the following code to read file: lines=file(data.txt).read().split(\n) I have the following
I have the following code which reads in the follow file, append a \r\n
I have a C# application that includes the following code: string file = relativePath.txt;
So I have the following code to check for the text file called Doctors.txt
I have the following code: <?php $FILE=giant-data-barf.txt; $fp = fopen($FILE,'r'); //read everything into data
I have written the following line of code that reads the data in a
I have the following code to read a .txt file and to put it
I have the following simple code, that reads contents of a text file into
I have the following code that reads the content of a url public static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.