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 Link to comment https://forums.phpfreaks.com/topic/38644-whats-the-best-way-to-modify-one-line-in-a-file/ 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? Link to comment https://forums.phpfreaks.com/topic/38644-whats-the-best-way-to-modify-one-line-in-a-file/#findComment-185503 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 Link to comment https://forums.phpfreaks.com/topic/38644-whats-the-best-way-to-modify-one-line-in-a-file/#findComment-185504 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"; } ?> Link to comment https://forums.phpfreaks.com/topic/38644-whats-the-best-way-to-modify-one-line-in-a-file/#findComment-185509 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"; } ?> Link to comment https://forums.phpfreaks.com/topic/38644-whats-the-best-way-to-modify-one-line-in-a-file/#findComment-185520 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. Link to comment https://forums.phpfreaks.com/topic/38644-whats-the-best-way-to-modify-one-line-in-a-file/#findComment-185589 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. Link to comment https://forums.phpfreaks.com/topic/38644-whats-the-best-way-to-modify-one-line-in-a-file/#findComment-185776 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! Link to comment https://forums.phpfreaks.com/topic/38644-whats-the-best-way-to-modify-one-line-in-a-file/#findComment-186893 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.