You are here: Re: deleting a line « PHP Programming Language « IT news, forums, messages
Re: deleting a line

Posted by amygdala on 04/11/07 22:44

"programming" <periklis.ioannou@gmail.com> schreef in bericht
news:1176327375.195579.189940@d57g2000hsg.googlegroups.com...
On Apr 11, 10:56 pm, Kim Andrι Akerψ <kiman...@NOSPAMbetadome.com>
wrote:
> programming wrote:
> > On Apr 11, 7:08 pm, Toby A Inkster <usenet200...@tobyinkster.co.uk>
> > wrote:
> > > programming wrote:
> > > > say i would like to delete the middle line of this txt, in
> > > > member.txt what php code or logic would help me accomplish this?
>
> > > For a small file:
>
> > > 1. Read member.txt into an array (the "file" function is
> > > your friend);
> > > 2. Make the changes to the array (the "unset" function is
> > > your friend);
> > > 3. Output the array to "member-new.txt";
> > > 4. Delete "member.txt";
> > > 5. Rename "member-new.txt" to "member.txt";
>
> > > If the file is larger (more than say, 500 kB) this might become a
> > > bit memory-hungry, and it's worth looking at iterating over each
> > > line of the file individually rather than reading it all into
> > > memory at once.
>
> > Thanks for you steps, i had a go at doing it..is this what you meant
> > by any chance?
>
> > <?php
>
> > $line = file ("./member.txt", "w");
> > unset($line);
>
> > $line = fopen("./member-new.txt");
>
> > unlink("./member.txt")
>
> > rename("member-new.txt","member.txt");
>
> > fclose($line);
>
> > ?>
>
> > I am getting a parse error, and i am not sure if i am doing this
> > right??
>
> > Parse error: syntax error, unexpected T_STRING in /volumes/data1/home/
> > pioannou/public_html/php/Assignment11/admin/delete_member.php on line
> > 11
>
> The parse error itself comes from the missing semicolon on the line
> with the unlink() function.
>
> Second, you have placed the "w" parameter on the wrong function (on the
> file() function instead of the fopen() function).
>
> Thirdly, you're attempting to move/rename the new file BEFORE closing
> the file handle for it. This could cause problems in terms of
> locking/permissions.
>
> Fourthly, you still haven't output the updated array to the new file
> yet. Also, you're unsetting (deleting) the entire array instead of just
> a single item.
>
> After fixing the errors, the final source code snippet should look
> something like this (with comments attached):
>
> <?php
>
> // Which line number (counting from 0) should we delete?
> $line_to_delete = 4;
>
> // Load the lines into an array; newlines are still attached
> $lines = file ("./member.txt");
>
> // Delete the line number from the array
> unset($lines[$line_to_delete]);
>
> // Open the new file for writing
> $lines_update = fopen("./member-new.txt", "w");
>
> // 1. Join the array to a single string (newlines are already attached)
> // 2. Write the updated array to the new file
> fwrite($lines_update, join("", $lines);
>
> // Close the file handle to allow other operations on the file
> fclose($lines_update);
>
> // Remove the old file
> unlink("./member.txt");
>
> // Rename the new file to use the old filename, replacing the old file
> rename("member-new.txt","member.txt");
>
> ?>
>
> --
> Kim Andrι Akerψ
> - kiman...@NOSPAMbetadome.com
> (remove NOSPAM to contact me directly)

> this sounds good, but the only think i am confused about know is the
> line_to_delete = 4;
> is there any chance of matching the entry line from my web page to the
> line
> in the text file by using a superglobal to catch the value that needs
> to be matched to the line in the file...

$line_to_delete = 'this is my test line.';

/*
// instead of:
unset($lines[$line_to_delete]);
// use:
*/

// deletes every occurence of the line, case insensitive
foreach($lines as $key => $value)
{
if(strtolower($value) == strtolower($line_to_delete))
{
unset($lines[$key]);
}
}

// or, deletes first occurence of the line, case insensitive
$found = false;
while (!$found && list($key, $value) = each ($lines)) {
if(strtolower($value) == strtolower($line_to_delete))
{
unset($lines[$key]);
$found = true;
}
}

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация