rei Posted July 11, 2006 Share Posted July 11, 2006 i have a file with the contents of :[quote]apple1orange2lime3strawberry4pineapple5mango6[/quote]i would like to insert one newline "newline" in between "lime3" and "strawberry4"first i wrote my code like this:[code]<?php$fp = fopen("data/file.txt", "r+");while($buf = fgets($fp)){ if(preg_match("/strawberry/", $buf)){ fputs($fp, "newline\n"); }}?>[/code]but it ended up like this:[quote]apple1orange2lime3strawberry4newlinee5mango6[/quote]so i changed my code to the following (omitting \n at the end of "newline")[code]<?php$fp = fopen("data/file.txt", "r+");while($buf = fgets($fp)){ if(preg_match("/strawberry/", $buf)){ fputs($fp, "newline"); }}?>[/code]but i got the result of[quote]apple1orange2lime3strawberry4newlinele5mango6[/quote]i cant seem to have a line inserted in between..it replaces the line after it..i would appreciate any helpthanks Quote Link to comment https://forums.phpfreaks.com/topic/14255-how-to-edit-files-contents-with-phpinsert-line-in-between/ Share on other sites More sharing options...
willfitch Posted July 11, 2006 Share Posted July 11, 2006 Well,If you are hard-coding your parameters with your if conditions, you could use file() and do a foreach() loop and loop through each line. Once you hit the strawberry, insert your new line.[code=php:0]<?php$file = file('file.txt');foreach($file as $line_number => $content) { if (trim($content) == 'strawberry') { // do your thing here. }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14255-how-to-edit-files-contents-with-phpinsert-line-in-between/#findComment-55986 Share on other sites More sharing options...
tomfmason Posted July 11, 2006 Share Posted July 11, 2006 This somewhat answers my question at [url=http://www.phpfreaks.com/forums/index.php/topic,100138.0.html]http://www.phpfreaks.com/forums/index.php/topic,100138.0.html[/url]. How would I specify then line number. Quote Link to comment https://forums.phpfreaks.com/topic/14255-how-to-edit-files-contents-with-phpinsert-line-in-between/#findComment-55991 Share on other sites More sharing options...
rei Posted July 11, 2006 Author Share Posted July 11, 2006 willfitch,thanks for the replybut i still get the result as following [quote]apple1orange2lime3strawberry4newlinele5mango6[/quote]my code is as following:[code]<?php$file = file("data/file.txt");foreach($file as $line_number => $contents){ if(trim($contents) == 'strawberry'){ fputs($fp, "newline"); }}?>[/code]is it that fputs isnt a good idea?[quote author=willfitch link=topic=100146.msg394895#msg394895 date=1152589232]Well,If you are hard-coding your parameters with your if conditions, you could use file() and do a foreach() loop and loop through each line. Once you hit the strawberry, insert your new line.[code=php:0]<?php$file = file('file.txt');foreach($file as $line_number => $content) { if (trim($content) == 'strawberry') { // do your thing here. }}?>[/code][/quote] Quote Link to comment https://forums.phpfreaks.com/topic/14255-how-to-edit-files-contents-with-phpinsert-line-in-between/#findComment-55992 Share on other sites More sharing options...
rei Posted July 11, 2006 Author Share Posted July 11, 2006 tomfmason,thanks but how to write to a specific line is still unanswered[quote author=tomfmason link=topic=100146.msg394901#msg394901 date=1152589789]This somewhat answers my question at [url=http://www.phpfreaks.com/forums/index.php/topic,100138.0.html]http://www.phpfreaks.com/forums/index.php/topic,100138.0.html[/url]. How would I specify then line number.[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/14255-how-to-edit-files-contents-with-phpinsert-line-in-between/#findComment-55998 Share on other sites More sharing options...
ShogunWarrior Posted July 11, 2006 Share Posted July 11, 2006 This will do the trick:[code]function insertOnLine($arr,$line,$text,$newline="\r\n"){ $slice = array_slice($arr,0,$line); $slice[] = $text; $newArr = array_merge($slice,array_slice($arr,$line)); return(implode($newline,$newArr));}[/code]You would use it like this:[code]$fContent = file('file.txt');$newContent = insertOnLine($fContent,4,"NEWLINE");[/code]You would go from this:[code]I'm1I'm2I'm3I'm4I'm5[/code]To This:[code]I'm1I'm2I'm3NEWLINEI'm4I'm5[/code]Hope it helps. Quote Link to comment https://forums.phpfreaks.com/topic/14255-how-to-edit-files-contents-with-phpinsert-line-in-between/#findComment-56129 Share on other sites More sharing options...
rei Posted July 28, 2006 Author Share Posted July 28, 2006 Shogun,thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/14255-how-to-edit-files-contents-with-phpinsert-line-in-between/#findComment-65046 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.