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

  • SEARCH
  • Home
  • 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 9208883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:42:49+00:00 2026-06-18T00:42:49+00:00

I have an issue with writing and reading to text file. I have to

  • 0

I have an issue with writing and reading to text file.

I have to first write from a text file to another text file some values which I need to read again. Below are the code snippets:

Write to text file:

$fp = @fopen ("text1.txt", "r");

$fh = @fopen("text2.txt", 'a+');
        
if ($fp) {
 //for each line in file
    while(!feof($fp)) {
    //push lines into array
    $thisline = fgets($fp);
    $thisline1 = trim($thisline);
        
     $stringData = $thisline1. "\r\n";
    fwrite($fh, $stringData);
                    
                  fwrite($fh, "test");

                }
            }
fclose($fp);
fclose($fh);

Read from the written textfile

$page = join("",file("text2.txt"));
$kw = explode("\n", $page);
for($i=0;$i<count($kw);$i++){
             
 echo rtrim($kw[$i]);
               
}

But, if I am not mistaken due to the "/r/n" I used to insert the newline, when I am reading back, there are issues and I need to pass the read values from only the even lines to a function to perform other operations.

How do I resolve this issue? Basically, I need to write certain values to a textfile and then read only the values from the even lines.

  • 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-18T00:42:51+00:00Added an answer on June 18, 2026 at 12:42 am

    I’m not sure whether you have issues with the even line numbers or with reading the file back in.

    Here is the solution for the even line numbers.

    $page = join("",file("text2.txt"));
    $kw = explode("\n", $page);
    for($i=0;$i<count($kw);$i++){
    
        $myValue = rtrim($kw[$i]);
        if(i % 2 == 0)
        {
            echo $myValue;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a weird issue with regards to getline while reading from file. Below
I have a 100 GB text file, which is a BCP dump from a
I have an application that imports data read from text files from a directory
I am writing an OS X application which uses NSSpeechSynthesizer to read text to
I'm having an issue reading from a java input stream. I have a buffer
I need to read and send email from pop3 server (Rackspace) I have done
This is the loop I have for reading from a file of any size,
Rrom C#, reading/writing over SOCKET to JAVA and having some concurrency/socket issue. I am
I'm writing a WP plugin and have an issue. In the pludin's settings area,
I have an issue of alphabetically sorting the contacts picked from the address book

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.