Jump to content

How to loop line by line.


Nyla
Go to solution Solved by Barand,

Recommended Posts

I'm so sorry to ask such a "newbee question," but believe me I have been on Google for the better part of the week trying to find an answer.

 

I'll start with the brief question.

Then I'll give an example to show what I mean.

Then, I'll give the BESTEST "pseudo-code" I could come up with (to PROVE that I've really given it my best).

 

Question:

How do I make PHP loop through a big file, make the changes line by line, and to save the resultant file.

 

Example: I have a 100mb text file ("animals.txt") with 500,000 lines. If any lines have the word "cat" in it, I want to add "Be careful with cats!" to the end of the line:

 

From this:

A fish and his tank.

A cat and his toy.

A bird and her cage.

A frog and his lilly.

A cat and her friend.

 

To this:

A fish and his tank.

A cat and his toy. Be careful with cats!

A bird and her cage.

A frog and his lilly.

A cat and her friend. Be careful with cats!

 

The best "pseudo-code" I can come up with is:

 

<?php

$data = file_get_contents("animals.txt");

$lines = explode(PHP_EOL,$data);
foreach($lines as $line){
if(stristr($line,'cat')) { $line = $line.' Be careful with cats!'; }
$result .= $line;
fwrite($result,'MyNewFixedFile.txt')
?>
 
I know this looks like a long drawn out question, but I worked really hard on trying to figure it out myself, and I worked really hard on making it concise and easy to understand. Would someone please tell me what to do to make this work? Hopefully, my "pseudo-code" is close to being correct. But if it's totally horrible, I don't have any ego in this one, so you can tell me it is horrible and I won't take it personally.
 
THANK YOU!!!
 
 
 
 
 
Link to comment
Share on other sites

If you used file_put_contents instead of fwrite(), you'd have yourself a winner.

 

But I'd do it a bit differently:

$lines = file("animals.txt");
foreach ($lines as $n => $line) {
    if (stripos($line, "cat") !== false) {
        $line = rtrim($line) . " Be careful with cats!" . PHP_EOL;
    }
}
file_put_contents("animals.txt", $lines);
If the file was much larger, though, (and 100MB may be too large already,) you'd need to use a different tactic: you'd run out of memory to store the entire file in PHP's memory.
Link to comment
Share on other sites

Uh-oh, for some reason, your code doesn't work (none of the lines get changed).

 

I thought it was because you have "$lines" plural, (so I made it $line), but it still doesn't work. 

 

I'm staring at it and staring at it, but my mind is as blank as a cloud right now, I can't figure it out?

Edited by Nyla
Link to comment
Share on other sites

Thank you, y'all! The code actually works:

 

<?php

$lines = file("animals.txt");
foreach ($lines as $n => $line) {
    if (stripos($line, "cat") !== false) {
        
        $lines[$n] = rtrim($line) . " Be careful with cats!" . PHP_EOL;
    }
}
file_put_contents("animals.txt", $lines);
 
This is the FIRST forum I have ever asked a question to where it just gets answered, I cannot believe it! Thank you all so much!
 
Usually, well, NOT "usually".... ALWAYS, this is what happens:
 
Me: How do I make PHP loop line through line?
1st response: "Why do you need to loop line by line?"
2nd response: "Google FOPEN"
3rd response: "This can be done in Perl"
4th response: "It's just simple loops"
Me: I cannot figure out how to loop line by line.
5th response: "What kind of project are you working on?"
Me: It's complicated, I just can't figure out how to loop.
6th response: "This isn't a 'free coding' help center, you need not be rude.
Me: I wasn't rude.
7th response [moderator]: Saying you're not rude, is rude. You are now banned
 
So, I think I've finally found a decent forum!
 
Thank you, everybody!
 
 
 
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.