php_joe Posted February 15, 2007 Share Posted February 15, 2007 I want to write a snipit that will modify one line in a text file without changing any of the other lines. I was thinking something like this: <? $newline = "new junk"; $linenumber = "3"; $file = "./textdocument.txt"; $line = file($file); foreach($line as $key => $value){ if($key == $linenumber) $value = $newline; } $newcontent = implode("\n", $line); $handle = fopen($file, "w"); fwrite($handle, $newcontent); fclose($handle); ?> Have I made a hash of this? Is there a better way of doing it? Maybe without exploding, looping, imploding, and rewriting all the lines? Thanks in advance! Joe Quote Link to comment Share on other sites More sharing options...
trq Posted February 15, 2007 Share Posted February 15, 2007 If your on Linux the best way would probably be to use sed. What exactly do you need to modify? Quote Link to comment Share on other sites More sharing options...
php_joe Posted February 15, 2007 Author Share Posted February 15, 2007 I'm using a Linux system, but I don't know 'sed'. I just want to be able to modify a line on a text document without changing. nothing really fancy. Something like this: Cat Dog Bunny and change it to: Cat Horse Bunny Quote Link to comment Share on other sites More sharing options...
trq Posted February 15, 2007 Share Posted February 15, 2007 <?php $file = "nameoffile.txt"; if (exec("sed -i -e 's/Dog/Horse/' $file")) { echo "Horse replaced Dog"; } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted February 15, 2007 Share Posted February 15, 2007 Oops... forgot. Bash will return 0 on success. I know it looks wrong but this will work. <?php $file = "nameoffile.txt"; $r = exec("sed -i -e 's/Dog/Horse/' $file"); if ($r == 0) { echo "Horse replaced Dog"; } ?> Quote Link to comment Share on other sites More sharing options...
php_joe Posted February 15, 2007 Author Share Posted February 15, 2007 Correct me if I'm wrong, but that searches for a line based on the text in the line, right? I want to single out a line by the line number and replace the text with the new text (the line # is the info I have, not the existing text). This is what I've trimmed it down to so far: <? $line = file($textdocument); $line[$line_number] = "$new_text\n"; $newcontent = implode("", $line); $handle = fopen($worldlist, "w"); fwrite($handle, $newcontent); fclose($handle); ?> And it seems to work now. Quote Link to comment Share on other sites More sharing options...
trq Posted February 15, 2007 Share Posted February 15, 2007 You could still use sed. <?php exec("sed -i -e '" . $linenumber . "d' $textdocument"); ?> And I'm sticking to it. Quote Link to comment Share on other sites More sharing options...
php_joe Posted February 17, 2007 Author Share Posted February 17, 2007 You could still use sed. <?php exec("sed -i -e '" . $linenumber . "d' $textdocument"); ?> And I'm sticking to it. Yes, that's what I was looking for. Thank you very much! Quote Link to comment 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.