Jump to content


Photo

how to edit file's contents with php(insert line in between)


  • Please log in to reply
6 replies to this topic

#1 rei

rei
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 11 July 2006 - 03:34 AM

i have a file with the contents of :

apple1
orange2
lime3
strawberry4
pineapple5
mango6


i would like to insert one newline "newline" in between "lime3" and "strawberry4"

first i wrote my code like this:
<?php

$fp = fopen("data/file.txt", "r+");

while($buf = fgets($fp)){
    if(preg_match("/strawberry/", $buf)){
        fputs($fp, "newline\n");
    }
}
?>
but it ended up like this:

apple1
orange2
lime3
strawberry4
newline
e5
mango6


so i changed my code to the following (omitting \n at the end of "newline")
<?php

$fp = fopen("data/file.txt", "r+");

while($buf = fgets($fp)){
    if(preg_match("/strawberry/", $buf)){
        fputs($fp, "newline");
    }
}
?>

but i got the result of

apple1
orange2
lime3
strawberry4
newlinele5
mango6


i cant seem to have a line inserted in between..
it replaces the line after it..

i would appreciate any help
thanks

#2 willfitch

willfitch
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 11 July 2006 - 03:40 AM

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.
<?php
$file = file('file.txt');
foreach($file as $line_number => $content) {
    if (trim($content) == 'strawberry') {
        // do your thing here.
    }
}
?>

Zend Certified Engineer (ZEND001989)
www.willfitch.com

#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 11 July 2006 - 03:49 AM

This somewhat answers my question at http://www.phpfreaks...c,100138.0.html. How would I specify then line number.

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#4 rei

rei
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 11 July 2006 - 03:53 AM

willfitch,
thanks for the reply
but i still get the result as following

apple1
orange2
lime3
strawberry4
newlinele5
mango6


my code is as following:
<?php
$file = file("data/file.txt");

foreach($file as $line_number => $contents){
    if(trim($contents) == 'strawberry'){
        fputs($fp, "newline");
    }
}
?>

is it that fputs isnt a good idea?

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.

<?php
$file = file('file.txt');
foreach($file as $line_number => $content) {
    if (trim($content) == 'strawberry') {
        // do your thing here.
    }
}
?>



#5 rei

rei
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 11 July 2006 - 04:03 AM

tomfmason,
thanks but how to write to a specific line is still unanswered

This somewhat answers my question at http://www.phpfreaks...c,100138.0.html. How would I specify then line number.



#6 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 11 July 2006 - 12:01 PM

This will do the trick:
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));
}

You would use it like this:
$fContent = file('file.txt');
$newContent = insertOnLine($fContent,4,"NEWLINE");

You would go from this:
I'm1
I'm2
I'm3
I'm4
I'm5

To This:
I'm1
I'm2
I'm3
NEWLINE
I'm4
I'm5

Hope it helps.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#7 rei

rei
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 28 July 2006 - 08:22 AM

Shogun,

thanks!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users