Nyla Posted November 20, 2014 Share Posted November 20, 2014 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!!! Quote Link to comment https://forums.phpfreaks.com/topic/292585-how-to-loop-line-by-line/ Share on other sites More sharing options...
requinix Posted November 20, 2014 Share Posted November 20, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/292585-how-to-loop-line-by-line/#findComment-1497058 Share on other sites More sharing options...
Nyla Posted November 20, 2014 Author Share Posted November 20, 2014 (edited) 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 November 20, 2014 by Nyla Quote Link to comment https://forums.phpfreaks.com/topic/292585-how-to-loop-line-by-line/#findComment-1497059 Share on other sites More sharing options...
Solution Barand Posted November 20, 2014 Solution Share Posted November 20, 2014 Change $line = rtrim($line) . " Be careful with cats!" . PHP_EOL; to $lines[$n] = rtrim($line) . " Be careful with cats!" . PHP_EOL; 1 Quote Link to comment https://forums.phpfreaks.com/topic/292585-how-to-loop-line-by-line/#findComment-1497079 Share on other sites More sharing options...
requinix Posted November 20, 2014 Share Posted November 20, 2014 Change $line = rtrim($line) . " Be careful with cats!" . PHP_EOL; to $lines[$n] = rtrim($line) . " Be careful with cats!" . PHP_EOL; I blame my sick boss for making me sick too and thus not quite in my right mind. But at least I did get the $n=>. Quote Link to comment https://forums.phpfreaks.com/topic/292585-how-to-loop-line-by-line/#findComment-1497123 Share on other sites More sharing options...
Barand Posted November 20, 2014 Share Posted November 20, 2014 I know the feeling. I can always fall back on "senility" when the need arises. Quote Link to comment https://forums.phpfreaks.com/topic/292585-how-to-loop-line-by-line/#findComment-1497135 Share on other sites More sharing options...
Nyla Posted November 21, 2014 Author Share Posted November 21, 2014 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! Quote Link to comment https://forums.phpfreaks.com/topic/292585-how-to-loop-line-by-line/#findComment-1497145 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.