473,418 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

deleting a line

how do i delete from a text file 1 of the following lines:
jon|scott
adam|smith <--delete
paul|clark

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?

Also i am clicking on a hyperlink that is keeping count of the entries
i have
made. e.g php?record_id =1
php?record_id =2

Apr 11 '07 #1
13 2416
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.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Apr 11 '07 #2
Hi,

you need to open two streams, one for reading, one for writing.

Be wary by more than one request at a time!!! ( errors!)

there is no delete function couse a file is only a stream ;)

Apr 11 '07 #3
programming wrote:
how do i delete from a text file 1 of the following lines:
jon|scott
adam|smith <--delete
paul|clark

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?

Also i am clicking on a hyperlink that is keeping count of the entries
i have
made. e.g php?record_id =1
php?record_id =2
Or, better yet, learn to use a SQL database.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 11 '07 #4
On Apr 11, 10:57 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
programming wrote:
how do i delete from a text file 1 of the following lines:
jon|scott
adam|smith <--delete
paul|clark
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?
Also i am clicking on a hyperlink that is keeping count of the entries
i have
made. e.g php?record_id =1
php?record_id =2

Or, better yet, learn to use a SQL database.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
yes, but this assignment i am doing does not require that...the next
one will be using SQL

Apr 11 '07 #5
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.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
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

Apr 11 '07 #6
programming wrote:
On Apr 11, 10:57 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>programming wrote:
>>how do i delete from a text file 1 of the following lines:
jon|scott
adam|smith <--delete
paul|clark
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?
Also i am clicking on a hyperlink that is keeping count of the entries
i have
made. e.g php?record_id =1
php?record_id =2
Or, better yet, learn to use a SQL database.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

yes, but this assignment i am doing does not require that...the next
one will be using SQL
Ah, getting help with your homework...

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 11 '07 #7
On Apr 11, 11:20 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
programming wrote:
On Apr 11, 10:57 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
programming wrote:
how do i delete from a text file 1 of the following lines:
jon|scott
adam|smith <--delete
paul|clark
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?
Also i am clicking on a hyperlink that is keeping count of the entries
i have
made. e.g php?record_id =1
php?record_id =2
Or, better yet, learn to use a SQL database.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
yes, but this assignment i am doing does not require that...the next
one will be using SQL

Ah, getting help with your homework...

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Yeah i am pleased with my progress with it so far, hopefully forums
can be a help as i progress

Apr 11 '07 #8
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ø
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Apr 11 '07 #9
Kim André Akerø wrote:
// 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);
Missing a closing parenthesis here, but otherwise, yes, something like
this. Though personally I'd check the return value from fwrite() before
you go and do this:
// 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");
--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Apr 11 '07 #10
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...

Apr 11 '07 #11

"programming" <pe**************@gmail.comschreef in bericht
news:11**********************@d57g2000hsg.googlegr oups.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;
}
}
Apr 11 '07 #12
On Apr 12, 8:44 am, "amygdala" <nore...@noreply.comwrote:
"programming" <periklis.ioan...@gmail.comschreef in berichtnews:11**********************@d57g2000hsg.g ooglegroups.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;
}

}
Thanks for the help. However, I am still having a little bit of
trouble with deleting a line from a txt
file. When i click on the hyper link that is matched to record?=
$count,
it still wont delete and append the details to the file...

here is the code that i am trying to implement. Are there any changes
i can make to this code to help me accomplish the task?

<?php
if(isset($_GET['record_id'])){
// Load the lines into an array; newlines are still attached
$line_to_delete = $_GET['record_id'];

$line=file("member.txt");

$found = false;
while(!$found && list($key, $value) = each($line)) {
if(strtolower($value) == strtolower($line_to_delete))
{
unset($line[$key]);
$found = true;
}
}
}
//Open the new file for writing
$lines_update = fopen("./member.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("", $line));
// Close the file handle to allow other operations on the file
fclose($lines_update);
// Remove the old file
?>

Cheers,
Peri

Apr 12 '07 #13
programming wrote:
while(!$found && list($key, $value) = each($line)) {
if(strtolower($value) == strtolower($line_to_delete))
{
unset($line[$key]);
$found = true;
}
}
Could be neater as:

while (list($key, $value) = each($line))
{
if (strtolower($value) == strtolower($line_to_delete))
{
unset($line[$key]);
break;
}
}

or even:

foreach ($line as $key=>$value)
{
if (strcasecmp($value, $line_to_delete)==0)
{
unset($line[$key])
break;
}
}

However, I think what you actually want might be:

foreach ($line as $linenumber=>$value)
{
if ($line_to_delete==$linenumber)
{
unset($line[$linenumber])
break;
}
}

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Apr 12 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: flupke | last post by:
Hi, i'm having trouble with deleting elements from a list in a for loop ============== test program ============== el = print "**** Start ****" print "List = %s " % el index = 0 for line...
5
by: Nora | last post by:
Hi, I have about 200 xml files which contain one line, that I want to delete. This line is always the last line of the file and it always begins with "<?Pub" Transformations don't work as due...
1
by: Mark | last post by:
Not sure this is the right place for this questions, but here goes: I get an error message when deleting an table from a Access database. The code is as follows and the error message is after...
6
by: Mark | last post by:
I get an error message when deleting rows from a table in Access database. My understanding is that the error message relates to the sharing of the Inetpub and the wwwroot directory. While I...
4
by: Mark | last post by:
Not sure this is the right place for this questions, but here goes: I get an error message when deleting an table from a Access database. The code is as follows and the error message is after...
1
by: Mark | last post by:
Not sure this is the right place for this questions, but here goes: I get an error message when deleting an table from a Access database. The code is as follows and the error message is after...
2
by: angus | last post by:
hello everybody, i'm having some problems searching a text file and then deleting the line. since i used fileopen(), i can use the eof() function to loop through the file, searching for the...
46
by: DP | last post by:
hi, i've got a form, with a subform in it. i've got a delete button in the subform. the code i;ve got is; Private Sub cmdDeleteRecord_Click() msg = "Are you sure you want to delete this...
4
by: sphinney | last post by:
I'm not exactly sure how to start this post. My question is pretty simple, but it will take a little bit of context before I can state it. (And thanks in advance for taking the time to read this!) ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.