Help - Search - Member List - Calendar
Full Version: remove specific line
WorkTheWeb Forums > Webmaster Resources > PHP Help
Support our Sponsors!
Ron Eggler @ work
Hi,

I got a textfile with e-mail addresses on every line. now I want to be able
to delete one e-mail-address from this file and shift everything below one
line up. how do I do that?
Thank you! :)

Kimmo Laine
"Ron Eggler @ work" <[Email Removed]> kirjoitti
viestiss:[Email Removed]...
QUOTE
Hi,

I got a textfile with e-mail addresses on every line. now I want to be
able
to delete one e-mail-address from this file and shift everything below one
line up. how do I do that?
Thank you! :)

<?php
// Read the file into an array. One address per row
$emails = file("emails.txt");
// Remove given email address
unset($emails[array_search($email_address)]);
// Change array back to end-of-line separated string, write to file
file_put_contents("emails.txt", implode("rn", $emails));
// In, out, wipe the tool, say "thank you", and leave. Simple and easy.
?>

--
"I am pro death penalty. That way people learn
their lesson for the next time." -- Britney Spears

[Email Removed]

Ron Eggler @ work
Kimmo Laine wrote:
QUOTE
"Ron Eggler @ work" <[Email Removed]> kirjoitti
viestiss:[Email Removed]...
Hi,

I got a textfile with e-mail addresses on every line. now I want to
be able
to delete one e-mail-address from this file and shift everything
below one line up. how do I do that?
Thank you! :)

<?php
// Read the file into an array. One address per row
$emails = file("emails.txt");
// Remove given email address
unset($emails[array_search($email_address)]);
// Change array back to end-of-line separated string, write to file
file_put_contents("emails.txt", implode("rn", $emails));
// In, out, wipe the tool, say "thank you", and leave. Simple and
easy.

Oh thank you very much! :)
But I get these Messages:

Warning: Wrong parameter count for array_search() in
/srv/www/htdocs/web2/html/php/nospam/pop.php on line 31

Fatal error: Call to undefined function: file_put_contents() in
/srv/www/htdocs/web2/html/php/nospam/pop.php on line 33

Ron Eggler @ work
Ron Eggler @ work wrote:
QUOTE
Kimmo Laine wrote:
"Ron Eggler @ work" <[Email Removed]> kirjoitti
viestiss:[Email Removed]...
Hi,

I got a textfile with e-mail addresses on every line. now I want to
be able
to delete one e-mail-address from this file and shift everything
below one line up. how do I do that?
Thank you! :)

<?php
// Read the file into an array. One address per row
$emails = file("emails.txt");
// Remove given email address
unset($emails[array_search($email_address)]);
// Change array back to end-of-line separated string, write to file
file_put_contents("emails.txt", implode("rn", $emails));
// In, out, wipe the tool, say "thank you", and leave. Simple and
easy.

Oh thank you very much! :)
But I get these Messages:

Warning: Wrong parameter count for array_search() in
/srv/www/htdocs/web2/html/php/nospam/pop.php on line 31

Fatal error: Call to undefined function: file_put_contents() in
/srv/www/htdocs/web2/html/php/nospam/pop.php on line 33

ah: php_info() says:
PHP Version 4.3.3
and file_put_contents() need php5. how to do it with php 4.3.3?
Thank you! :)

Kimmo Laine
"Ron Eggler @ work" <[Email Removed]> wrote in message
news:[Email Removed]...
QUOTE
Kimmo Laine wrote:
"Ron Eggler @ work" <[Email Removed]> kirjoitti
viestiss:[Email Removed]...
Hi,

I got a textfile with e-mail addresses on every line. now I want to
be able
to delete one e-mail-address from this file and shift everything
below one line up. how do I do that?
Thank you! :)

<?php
// Read the file into an array. One address per row
$emails = file("emails.txt");
// Remove given email address
unset($emails[array_search($email_address)]);
// Change array back to end-of-line separated string, write to file
file_put_contents("emails.txt", implode("rn", $emails));
// In, out, wipe the tool, say "thank you", and leave. Simple and
easy.

Oh thank you very much! :)
But I get these Messages:

Warning: Wrong parameter count for array_search() in
/srv/www/htdocs/web2/html/php/nospam/pop.php on line 31

Sorry, my mistake... replace
unset($emails[array_search($email_address)]);
with
unset($emails[array_search($email_address, $emails)]);

QUOTE
Fatal error: Call to undefined function: file_put_contents() in
/srv/www/htdocs/web2/html/php/nospam/pop.php on line 33

Hmm file_put_contents is fairly new function, available since PHP5. It is
seems that you're running the script with older php. Doesn't matter, that's
just a shortcut for:

if(@$handler = fopen("emails.txt","w")){
foreach($emails as $line) fputs($handler, "$linern", 512);
fclose($handler);
}

--
Welcome to Usenet! Please leave tolerance, understanding
and intelligence at the door. They aren't welcome here.
eternal piste erection miuku gmail piste com

Ron Eggler
Kimmo Laine wrote:
[snip]
QUOTE
Hmm file_put_contents is fairly new function, available since PHP5.
It is seems that you're running the script with older php.
yup, php 4.3.3
Doesn't
matter, that's just a shortcut for:

if(@$handler = fopen("emails.txt","w")){
foreach($emails as $line) fputs($handler, "$linern", 512);
fclose($handler);
}

But following function doesn't remove the passed string ($email_address) out
of the textfile ($blacklist)
why not?
CODE

function removeEMail($email_address)
 {
 // Read the file into an array. One address per row
 $emails = file($blacklist);
 // Remove given email address
 unset($emails[array_search($email_address,$emails)]);
 // Change array back to end-of-line separated string, write to file
 if(@$handler = fopen($blacklist,"w"))
   {
   foreach($emails as $line)
     fputs($handler, "$linern", 512);
   fclose($handler);
   }
 echo $email_address." removed from ". $blacklist;
 }

and I still get following warning:
Warning: array_search(): Wrong datatype for second argument in
/srv/www/htdocs/web2/html/php/nospam/pop.php on line 31
how to remove it?
Thank you!

--
roN
www.rideon.ch

Kimmo Laine
"Ron Eggler" <[Email Removed]> kirjoitti
viestiss:[Email Removed]...
QUOTE
Kimmo Laine wrote:
[snip]
Hmm file_put_contents is fairly new function, available since PHP5.
It is seems that you're running the script with older php.
yup, php 4.3.3
Doesn't
matter, that's just a shortcut for:

if(@$handler = fopen("emails.txt","w")){
foreach($emails as $line) fputs($handler, "$linern", 512);
fclose($handler);
}

But following function doesn't remove the passed string ($email_address)
out of the textfile ($blacklist)


I'll just copy below, what I answered to you a couple of days ago, when you
were opening another file inside a function...

-------
$blacklist has not been defined, or it is outside the variablescope. I'm
guessing you've defined $blacklist elsewhere in the code. The easiest thing
to do is define $blacklist global. Just insert the line

global $blacklist;

at the first line of the funtion, before $file=fopen(.... That way the
function "sees" the variable as well, and therefore knows what to open.
-------

Honestly, Ron... I thought you got it the first time?

--
"I am pro death penalty. That way people learn
their lesson for the next time." -- Britney Spears

[Email Removed]

Ron Eggler
Kimmo Laine wrote:
[snip]
QUOTE
Honestly, Ron... I thought you got it the first time?
yes Laine, I'm sorry. I defined it global right after the definition of the

var so it is global everywhere.
and the removal still doesn't work.... :(

--
roN
www.rideon.ch

Kimmo Laine
"Ron Eggler" <[Email Removed]> kirjoitti
viestiss:[Email Removed]...
QUOTE
Kimmo Laine wrote:
[snip]
Honestly, Ron... I thought you got it the first time?
yes Laine, I'm sorry. I defined it global right after the definition of
the var so it is global everywhere.
and the removal still doesn't work.... :(


No, it isn't. Unless you have declared it global _inside_ the function, it
isn't available _inside_ the function. Put the global $blacklist; inside the
function and try again. If it still doesn't work, I don't know what will...

This from the manual, the chaper about variable scope: "In PHP global
variables must be declared global inside a function if they are going to be
used in that function."

Just in case there is still something strange about it, check the manual:
http://www.php.net/manual/en/language.variables.scope.php - it's explained
well there.

--
"I am pro death penalty. That way people learn
their lesson for the next time." -- Britney Spears

[Email Removed]

Ron Eggler @ work
[snip]
QUOTE
If it still doesn't
work, I don't know what will...

oh very bad because it doesn't work...: :( I pasted the Code below....
QUOTE

This from the manual, the chaper about variable scope: "In PHP global
variables must be declared global inside a function if they are going
to be used in that function."

yup, and the code looks like this:
CODE

//at the top of the file:
$blacklist="blacklist.txt";
//--------------------------------------------------------------------------
----
function removeEMail($email_address)
 {
 global $blacklist;
 // Read the file into an array. One address per row
 $emails = file($blacklist);
 // Remove given email address
 unset($emails[array_search($email_address,$emails)]);
 // Change array back to end-of-line separated string, write to file
 if(@$handler = fopen($blacklist,"w"))
   {
   foreach($emails as $line)
     fputs($handler, "$linen", 512);
   fclose($handler);
   }
 echo $email_address." removed from ". $blacklist ."<br>n";
 }


Ron Eggler @ work
Ron Eggler @ work wrote:
QUOTE
[snip]
If it still doesn't
work, I don't know what will...

oh very bad because it doesn't work...: :( I pasted the Code below....

This from the manual, the chaper about variable scope: "In PHP global
variables must be declared global inside a function if they are going
to be used in that function."

yup, and the code looks like this:
CODE

//at the top of the file:
$blacklist="blacklist.txt";

//--------------------------------------------------------------------------
----
function removeEMail($email_address)
{
global $blacklist;
// Read the file into an array. One address per row
$emails = file($blacklist);
// Remove given email address
unset($emails[array_search($email_address,$emails)]);
// Change array back to end-of-line separated string, write to file
if(@$handler = fopen($blacklist,"w"))
{
foreach($emails as $line)
fputs($handler, "$linen", 512);
fclose($handler);
}
echo $email_address." removed from ". $blacklist ."<br>n";
}


oh now I see when writing the array back to file it adds about 16 empty
returns between every address... :( why that?

Kimmo Laine
"Ron Eggler @ work" <[Email Removed]> wrote in message
news:[Email Removed]...
QUOTE
Ron Eggler @ work wrote:
[snip]
If it still doesn't
work, I don't know what will...

oh very bad because it doesn't work...: :( I pasted the Code below....

This from the manual, the chaper about variable scope: "In PHP global
variables must be declared global inside a function if they are going
to be used in that function."

yup, and the code looks like this:
CODE

//at the top of the file:
$blacklist="blacklist.txt";

//--------------------------------------------------------------------------
----
function removeEMail($email_address)
{
global $blacklist;
// Read the file into an array. One address per row
$emails = file($blacklist);
// Remove given email address
unset($emails[array_search($email_address,$emails)]);
// Change array back to end-of-line separated string, write to file
if(@$handler = fopen($blacklist,"w"))
{
foreach($emails as $line)
fputs($handler, "$linen", 512);
fclose($handler);
}
echo $email_address." removed from ". $blacklist ."<br>n";
}


oh now I see when writing the array back to file it adds about 16 empty
returns between every address... :( why that?



Stupid me. I didn't remember that file() leaves the end of lines in the
strings, so adding them is unnecessary.

Change fputs($handler, "$linen", 512);
to -> fputs($handler, "$line", 512);

Maybe now...

I should rtfm... It says right there: "Each element of the array corresponds
to a line in the file, *with the newline still attached*".

Because of this, each time you saved the file, it doubled the lines... Thus
one line and 15 empty lines...

--
Welcome to Usenet! Please leave tolerance, understanding
and intelligence at the door. They aren't welcome here.
eternal piste erection miuku gmail piste com


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.