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! :)
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! :) |
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. |
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 |
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 |
QUOTE |
Fatal error: Call to undefined function: file_put_contents() in /srv/www/htdocs/web2/html/php/nospam/pop.php on line 33 |
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); } |
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; } |
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) |
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 |
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.... :( |
QUOTE |
If it still doesn't work, I don't know what will... |
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." |
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"; } |
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:
|
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:
oh now I see when writing the array back to file it adds about 16 empty returns between every address... :( why that? |