Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-02-2012, 02:09 AM   PM User | #1
Vernk
Regular Coder

 
Join Date: Oct 2011
Posts: 113
Thanks: 9
Thanked 1 Time in 1 Post
Vernk is an unknown quantity at this point
Delete certain line in txt file

Hello I am trying to delete a certain line in a txt file using PHP here is an example
data.txt
PHP Code:
Red
Blue
Green
Yellow
Orange 
Lets say I want to delete Green
I want it to delete the line and do NOT leave a blank white space

End result
PHP Code:
Red
Blue
Yellow
Orange 
Vernk is offline   Reply With Quote
Old 10-02-2012, 02:43 AM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
This goes back to the discussion of using MySQL.

You're using what is called "flat-file" database.
Everything you do (add, edit, delete) will involve several lines of code.
Way more work than MySQL, but it is what it is.

Something like below ... but not exactly ... just to give you an idea.

PHP Code:

// you already have an array from previous read of the file.
// pretend your array is called,  array1.

// let's say you want to delete "Red".
$delete="Red";

// open up your database (file) to write ... this is going to overwrite your file ...
$filename="data.txt";
$fh fopen($filename'w') or die("can't open file");

$count_array=count($array1);
for (
$i=0;$i<$count_array;$i++) {
$temp=explode("|",$array1[$i]);
$color=$temp[0];
$url=$temp[1];

if(
$delete){
if(
$delete == $color){
// do nothing
// this is the one to delete, so don't write this one to the file.
}
else{
fwrite($fh"$color|$url|\n");
}
}
// if delete

}//for loop
fclose($fh); 
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
Vernk (10-02-2012)
Old 10-02-2012, 02:52 AM   PM User | #3
Vernk
Regular Coder

 
Join Date: Oct 2011
Posts: 113
Thanks: 9
Thanked 1 Time in 1 Post
Vernk is an unknown quantity at this point
Looks like that code is deleting everything
Vernk is offline   Reply With Quote
Old 10-02-2012, 04:22 AM   PM User | #4
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Change this:
if($delete == $color){

To this:
if($delete === $color){

Not sure why that should make a difference, but see if it does.
mlseim is offline   Reply With Quote
Old 10-02-2012, 03:04 PM   PM User | #5
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
This will work if you're looking for a specific value (ie: Green):
PHP Code:
// Grab the file's rows in an array
$rows file'./data.txt' );

// look for `Green` (we trim all values because trailing line breaks, etc, are included)
$key array_search'Green'array_map'trim'$rows ) );

// Does the key exist? Remove it and rewrite the data
if ( FALSE !== $key )
{
    unset( 
$rows$key ] );
    
file_put_contents'./data.txt'implode''$rows ) );

This will work if you're looking for a specific line number:
PHP Code:
// Grab the file's rows in an array
$rows file'./data.txt' );

// Does line 3 (offset 2) exist? Remove it and rewrite the data
if ( isset( $rows[2] ) )
{
    unset( 
$rows[2] );
    
file_put_contents'./data.txt'implode''$rows ) );

__________________
ZCE

Last edited by kbluhm; 10-02-2012 at 03:13 PM..
kbluhm is offline   Reply With Quote
Users who have thanked kbluhm for this post:
Vernk (10-02-2012)
Old 10-02-2012, 03:12 PM   PM User | #6
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
yea ... do what kbluhm has.

my way is not so efficient.
mlseim is offline   Reply With Quote
Old 10-02-2012, 03:53 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This won't work though (for what you want): file_put_contents( './data.txt', implode( '', $rows ) );. That will write concurrently on a single line, not over multiple lines so it won't work in the next iteration of a run. Use this for the implode: implode(PHP_EOL, $rows) instead.
No matter what you do in a file handle, you cannot just yank a line. You need to yank it and overwrite the entire data in the existing file. This is why a database is the best option.
Fou-Lu is offline   Reply With Quote
Old 10-02-2012, 03:58 PM   PM User | #8
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
Read the comments... file() will return each line as a whole, including new lines.
PHP Code:
// look for `Green` (we trim all values because trailing line breaks, etc, are included)
$key array_search'Green'array_map'trim'$rows ) ); 
There is a flag you can pass to file() to avoid this, FILE_IGNORE_NEW_LINES, but I just left them intact for my examples.
__________________
ZCE

Last edited by kbluhm; 10-02-2012 at 04:20 PM..
kbluhm is offline   Reply With Quote
Old 10-02-2012, 04:10 PM   PM User | #9
Vernk
Regular Coder

 
Join Date: Oct 2011
Posts: 113
Thanks: 9
Thanked 1 Time in 1 Post
Vernk is an unknown quantity at this point
PHP Code:
if (isset($_POST['delsite'])) {
//$sstring = $_POST['ids'];
$delete $_POST['ids'];
$rows file('data.txt');

// look for `Green` (we trim all values because trailing line breaks, etc, are included)
$key array_search$deletearray_map'trim'$rows ) );

// Does the key exist? Remove it and rewrite the data
if ( FALSE !== $key )
{
    unset( 
$rows$key ] );
    
file_put_contents'data.txt'implode''$rows ) );

header("location:?access");

It doesn't do anything
Vernk is offline   Reply With Quote
Old 10-02-2012, 04:17 PM   PM User | #10
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
What doesn't it do?

$_POST['ids']... `ids` is plural which suggests multiple values. We'll need more info than "it doesn't do anything". ;)
__________________
ZCE
kbluhm is offline   Reply With Quote
Old 10-02-2012, 04:50 PM   PM User | #11
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by kbluhm View Post
Read the comments... file() will return each line as a whole, including new lines.
PHP Code:
// look for `Green` (we trim all values because trailing line breaks, etc, are included)
$key array_search'Green'array_map'trim'$rows ) ); 
There is a flag you can pass to file() to avoid this, FILE_IGNORE_NEW_LINES, but I just left them intact for my examples.
Oh yes I see what you mean. Since you file without removing the linefeeds, then you still have the linefeeds while imploding.
I'm not sure I like that.
Fou-Lu is offline   Reply With Quote
Old 10-02-2012, 06:01 PM   PM User | #12
Vernk
Regular Coder

 
Join Date: Oct 2011
Posts: 113
Thanks: 9
Thanked 1 Time in 1 Post
Vernk is an unknown quantity at this point
Code:
data.txt
1|MinecraftServers.net
2|somethingelse.com
3|another.com
$_POST['ids'] selected from <select name="ids">
options so after they select from the drop down

$_POSt['ids'] would be ID|website.com

So it's trying to delete the line 2|somethingelse.com
Vernk is offline   Reply With Quote
Old 10-02-2012, 06:09 PM   PM User | #13
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
And nor should you expect this would work. Your criteria was scalar; it had no indication of a key | value relationship in the file so you cannot search using something as simple as there is here.

This now requires you to [s|f]scanf / list / explode the data and separate the "key" out so you can find where it is in the file. If they are always numerically incremented matching the line number, then you can still use $key - 1 as whatever the posted line number is instead of using an array_search. Kbluhm's post in #5 there shows how to do that with just a line number.

Is there a reason why you don't simply use a db for this? Issuing a delete command would be a lot easier.
Fou-Lu is offline   Reply With Quote
Old 10-02-2012, 06:26 PM   PM User | #14
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
We all mentioned MySQL.

I can only assume it's the learning-curve issue that comes with it.

Most likely there will be more things added to the database, and
there will be more "updates" to the data. Security, or lack of,
by using .txt files may also become an issue. We can advise all day
long, but OP needs to decide if learning MySQL will suit him/her best.
mlseim is offline   Reply With Quote
Old 10-02-2012, 08:49 PM   PM User | #15
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
I went into my "php code vault" and came up with this code I used for spell checking. It uses the browsers spell checking features to check my text before I enter it into the database.

Create a spellcheck.txt and the html form reads the contents into the text field, you then edit the form and submit to re-write the txt file. I don't have php running to test it, but it was working the last time I used it.

spellcheck.php
PHP Code:
<?php
if(isset($_POST['update'])) {

$update $_POST['update'];
$fh fopen('spellcheck.txt''w') or die("can't open file");
fwrite($fh$update);
fclose($fh);

}
?>
<head>
<style type="text/css">
textarea {
font-size: 24px;
width: 800px;
height:500px;
}
</style>
</head>
<body>
<center>
<form method="post" action="spellcheck.php">
<textarea name="update">
<?php
echo file_get_contents("spellcheck.txt");
?>
</textarea><br><br>
<input type="submit" value="Submit" name="submit">
</form>
</center>
</body>
</html>


----
__________________
Leonard Whistler

Last edited by Len Whistler; 10-02-2012 at 09:09 PM..
Len Whistler is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:30 PM.


Advertisement
Log in to turn off these ads.