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 7071975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:45:08+00:00 2026-05-28T05:45:08+00:00

I’m having a problem with arrays and file writing, what I want to do

  • 0

I’m having a problem with arrays and file writing, what I want to do is take one file, and copy it onto another file, except with formatting added to it.

To be specific, this:

Line 1
Line 2
Line 3

Would become this:

<br /><hr />Line 1  
<br /><hr />Line 2  
<br /><hr />Line 3     

And I’ve sorta done that, but something weird happens. Instead of formatting all on one line, it linebreaks and keeps going. Like this

<br />1
THE END<br />7
THE END<br />0
THE END<br />Red
THE END<br />Silent
THE END<br />No ChangesTHE END   

My code for this is:

<?php

 $filename1 = "directorx_OLDONE.txt";
 $filename2 = "directorx_NEWONE.txt";
 $file1 = fopen($filename1, "r") or exit ("No");
 $file2 = fopen($filename2, "w") or exit ("No");

 while (!feof($file1)){
      $listArrayf1[] = fgets($file1);
 }
 fclose($file1);

 echo var_dump($listArrayf1) . "<br /><br />";


 $entries = count($listArrayf1) - 1;

 echo $entries;

 for($i=0;$i<=$entries;$i++){
      $listArrayf2[] = "<br />".$listArrayf1[$i]."THE END";
      fwrite($file2, $listArrayf2[$i]);
 }
 fclose($file2); 

 echo var_dump($listArrayf2);
 /*

 Open file1, r
 Open file2, w
 While it's not the end of the file, add each line of file1 to an array.
 Count the number of lines in file1 and call it Entries. -1 because gotta start at 0.

 Make a new array with the values of the old one, but with tags before and after it.

 */
 ?>

I’m sure there’s a better way to accomplish the ultimate goal I’m trying, which is detecting certain words entered into a form, (There’s probably a better way than making a formatted and non-formatted copy of what gets entered.) but my PHP vocab is limited and I’d like to figure out the long, prerequisite hard ways before learning how to do em easier.

At first I thought that it was because I was writing the OLDFILE manually, using the return key. So I made a script to write it using \n instead and it changed nothing.

  • 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-05-28T05:45:09+00:00Added an answer on May 28, 2026 at 5:45 am

    Eh 🙂

    At first, please take a look on var_dump and check what the function is returning (nothing, so correct usage is var_dump( ...); echo "<br />";)

    Second, fgets reads string including newline character, so I guess you see in your string something like this:

    string( 10) "abcdefghi
    "
    

    So you have to remove newline manually for example with trim.

    Next, I’d recommend to (at least) take a look at foreach.

    So I’d wrote the whole loop as:

    foreach( $listArrayf1 as $row){
      $row = "<br /><hr />". trim( $row)."THE END";
      fwrite($file2, $row);
      $listArrayf2[] = $row;
    }
    

    You may also use foreach( $listArrayf1 as &$row) and in the end $listArrayf1 will contain exactly the same as $listArrayf2. When you need to preserve all other spaces, you should probably use $row = substr( $row, 0, -1);

    btw: you can write normal code, mark it in textarea and by hitting ctrl+k it’ll get indented by 4 spaces

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
i want to parse a xhtml file and display in UITableView. what is the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am reading a book about Javascript and jQuery and using one of the
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The

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.