visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: inserting a line of text into the middle of a file


By: progGoon U.S.A.  Date: 14/04/2003 00:00:00  English  Points: 300 Status: Answered
Quality : Excellent
Yo,
I need to insert a line into the middle of a text file.
I'm finding-out where to insert the line by searching for a particular line.
I then want to insert a line UNDER the line I just found in the text file.
I can get it to work okay but instead of just writing the line, it also writes-over the line below it.

How can I simply insert a new line and type it it without destroying whats below it?
By: VGR Date: 14/04/2003 04:42:00 English  Type : Comment
how do you do please ?

that's a classical problem.

the only solution, if files are not too big, is :
-read old file until position found
-write in new file
-add new line in new file
-read rest of old file
-write in new file
-close both
-(optional) rename the one the other
By: TheFalklands Date: 15/04/2003 21:53:00 English  Type : Comment
If the entire file is small enough, load the whole thing into an array you can use all sorts of regexps to search, sort and modify the data. I've used this approach to search through remark lines inside PHP files and to look for bad HTML.

$file=file(name);
$output = array();
if ($file):
foreach($file as $line):
if (-Check for whatever you're looking for here-):
array_push($output, $line);
// Found the line, add new line after it //
array_push($output, 'ADDED LINE');
else:
array_push($output, $line);
endif;
endforeach;
endif;

At the end of this you have the array $output which has the lines of the old file with the inserted line. Use fopen() and fwrite() to send each line to a new file or delete and rewrite the old file.
By: bljak Date: 15/04/2003 23:25:00 English  Type : Comment
lets see, lets see

<?php
$fp = fopen("your_file","r");
$new_file = "";
$got_line = FALSE;
while(!feof($fp))
{
//lets assume file has max 1024 chars per line
$cnt_line = fgets($fp,1024);
$new_file .= $cnt_line;
//here comes the match you want
if("line you search for" == $cnt_line || !$got_line)
{
$got_line = TRUE;
$new_file .= "line you want under line you search after\n";
}
}
fclose($fp);
?>

$new_file will contain your new text file
i assumed also that you are looking for ONE line and ONCE, no matter if they are 2 times in text, thats why $got_line var, if you want your line to be displayed EACh time the line you match is found then use following

<?php
$fp = fopen("your_file","r");
$new_file = "";
while(!feof($fp))
{
//lets assume file has max 1024 chars per line
$cnt_line = fgets($fp,1024);
$new_file .= $cnt_line;
//here comes the match you want
if("line you search for" == $cnt_line)
{
$new_file .= "line you want under line you search after\n";
}
}
fclose($fp);
?>

if you want to pack all stuff in function, i will post it also :P

//bljak
By: bljak Date: 15/04/2003 23:31:00 English  Type : Comment
Ah of course, missed some things, you do basically this then after ANY of codes above
unlink("your_file");
$fw = fopen("your_file","w+");
fwrite($fw, $new_file);
fclose($fw);

have fun

//bljak
By: bljak Date: 15/04/2003 23:33:00 English  Type : Comment
And if you want it more complicated in one function and if you want not to have such LARGE variables, just need to say, i have solution for that also :)

//bljak
By: progGoon Date: 16/04/2003 00:23:00 English  Type : Comment
cool. I appreciate all the info. Let me take a look at these, try them out and I'll pick a winner.

lates,

By: VGR Date: 16/04/2003 01:18:00 English  Type : Comment
what bljak implemented basically is also basically what I wrote (no surprise! :D ), but I wouldn't delete (unlik()) the opriginal file before rewriting it. What would happen in case of an error (any error) during this final writing ?

That's why I propsoed to write a new file, rename the old one ( add .old to its name for example), then write the new one, and THEN ONLY (and if necessary, 'cause it's kinbd of a backup you've got there) delete the *.old file

"maximum-security is the key to success"
anti-Murphy's law
By: bljak Date: 16/04/2003 01:32:00 English  Type : Comment
Well
you wrote:
-read old file until position found
-write in new file
-add new line in new file

i do read and write, at same time, just i add the line after right one is found :P

about unlinking, no comment, i strictly follow the murphys law (read comment about that i could supply more compilcated code, and that one would more o r less base on that what you wrote, at least last 2 points :))

//bljak, the follower of Murphy

By: VGR Date: 16/04/2003 01:58:00 English  Type : Comment
of course I would have read the whole file at once too ;-)
it was "logically" speaking. Logically speaking, you're interested only in the first part at some point,n then you need the second one... But of course practically I would have read the file at once, I'm no fool :D
By: bljak Date: 16/04/2003 02:11:00 English  Type : Comment
Noone said you are, BUT ...
we both together expirienced how are users here, you can not explain them things, until you draw them each single detail. Show code in our cases actually. And still, they are not able to find for example our typos like one more bracers closing or such (due their weird code, but anyway). Well, you know what i want to say :)

//bljak
By: VGR Date: 16/04/2003 02:32:00 English  Type : Comment
yes, this explains why I sometimes don't have my comments accepted as answers. People want us to divinate their code and perform perfect modifications, and package this with a red ribbon.

Usually I only give the algorithm or general guidelines with functions' names and reference, 'cause I believe in self-learning and thought people coming here were programmers wanting external "fresh-eye" help on the manner to do things, not non-programmers seeking people to code for them for free.
By: bljak Date: 16/04/2003 02:45:00 English  Type : Comment
Yes, unfortunately

//bljak
By: progGoon Date: 16/04/2003 02:53:00 English  Type : Comment
VGR you said it first. I wasn't expecting any code, I just needed to understand the concept.
By: VGR Date: 16/04/2003 03:10:00 English  Type : Answer
I wasn't writing about you 8-))

sorry if you took it for you :D

please don't, this was just a private discussion with bljak

is it ok for you regarding this question ?
By: progGoon Date: 16/04/2003 03:14:00 English  Type : Comment
yeah it was great! I got my program to write correctly in the text files as well. Thank you for your advice.
By: VGR Date: 16/04/2003 03:19:00 English  Type : Comment
was this a 25 points question ?
I think bljak deserves to get some, so I'll share with him if you don't mind.
By: bljak Date: 16/04/2003 03:36:00 English  Type : Comment
Bah,
screw the points, am not on points hunt here, want actually to help some people. More i want it, more and more am disappointed what they all require. Must say, doesn't match for all, like for progGoon. But most other people, do nto want to learn, they want to use this for own business, earning money with code we apply. And yet, am trying to convince myself they will ask WHY is code written like that. Impossible mission. Sad.

//bljak

Do register to be able to answer

EContact
browser fav
page generated in 370.052810 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page