PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Jul 09, 2020 10:21 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Sun Mar 13, 2005 11:33 am 
Offline
Forum Newbie

Joined: Sun Mar 06, 2005 2:09 pm
Posts: 12
Hello all, I am working on a little PHP project, and what i would like to do, is not to "append" text to the end of a file, not to write text to the begginging of the file, and not to overwrite the file, but I want to:

insert text into an arbatrary line of a file, not erasing anything else, just pushing the rest down a line.

This must be possible without saving it all into an array and putting it back line by line and inserting the extra one where i want it. There must be a better way. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 13, 2005 11:43 am 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 13, 2005 4:05 pm 
Offline
Forum Newbie

Joined: Sun Mar 06, 2005 2:09 pm
Posts: 12
wow. ok then. looks like PHP could use an update.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 13, 2005 4:15 pm 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
I don't see why. The need for such a function is probably pretty low, as most situations want to either prepend or append to a file. At any rate, the language would do pretty much exactly what you'd have to do anyways.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 13, 2005 4:28 pm 
Offline
DevNet Master
User avatar

Joined: Mon Oct 25, 2004 9:29 pm
Posts: 3698
Location: New Jersey, US


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 13, 2005 5:40 pm 
Offline
Forum Newbie

Joined: Sun Mar 06, 2005 2:09 pm
Posts: 12
I came up with a working solution using arrays. I did the inserting manually though. I didn't look into array functions all that much. Any way it works.

Syntax: [ Download ] [ Hide ]
$date = date("U");

            $title = stripslashes($_POST['title']);

            $author = stripslashes($_POST['author']);

            $article = stripslashes($_POST['article']);

            $list = stripslashes($_POST['list']);

            $file = fopen($date , "a" );

            fwrite($file, "1\n" . $title . "\n" . $author . "\n" . $article);

// ====array part====  

            $array=file($list);

            $countc=count($array);

            $times=0;

            $fileb=fopen($list,"w");

            while ( $times <= $countc ) {

                fwrite($fileb, $array[$times]);

                if ($times == 2) {

                    fwrite($fileb, $date . "\n" . $title . "\n" . $author . "\n\n");

                }

                $times++;

            }



feyd | Please use
Syntax: [ Download ] [ Hide ]
and
Syntax: [ Download ] [ Hide ]
tags where approriate when posting code. Read:  </span>


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group