Jump to content

Archived

This topic is now archived and is closed to further replies.

rei

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

Recommended Posts

i have a file with the contents of :
[quote]
apple1
orange2
lime3
strawberry4
pineapple5
mango6
[/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]
apple1
orange2
lime3
strawberry4
newline
e5
mango6
[/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]
apple1
orange2
lime3
strawberry4
newlinele5
mango6
[/quote]

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

i would appreciate any help
thanks

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
willfitch,
thanks for the reply
but i still get the result as following
[quote]
apple1
orange2
lime3
strawberry4
newlinele5
mango6
[/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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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'm1
I'm2
I'm3
I'm4
I'm5
[/code]

To This:
[code]
I'm1
I'm2
I'm3
NEWLINE
I'm4
I'm5
[/code]

Hope it helps.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.