Professional Web Applications Themes

remove specific line - PHP Development

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! :)...

  1. #1

    Default remove specific line

    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! :)


    Ron Guest

  2. #2

    Default Re: remove specific line

    "Ron Eggler work" <com> kirjoitti
    viestissä:net... 

    <?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("\r\n", $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

    com


    Kimmo Guest

  3. #3

    Default Re: remove specific line

    Kimmo Laine wrote: 
    >
    > <?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("\r\n", $emails));
    > // In, out, wipe the tool, say "thank you", and leave. Simple and
    > easy.[/ref]

    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 Guest

  4. #4

    Default Re: remove specific line

    Ron Eggler work wrote: 
    >>
    >> <?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("\r\n", $emails));
    >> // In, out, wipe the tool, say "thank you", and leave. Simple and
    >> easy.[/ref]
    >
    > 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[/ref]

    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! :)


    Ron Guest

  5. #5

    Default Re: remove specific line

    "Ron Eggler work" <com> wrote in message
    news:net... 
    >>
    >> <?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("\r\n", $emails));
    >> // In, out, wipe the tool, say "thank you", and leave. Simple and
    >> easy.[/ref]
    >
    > 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[/ref]

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

    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, "$line\r\n", 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


    Kimmo Guest

  6. #6

    Default Re: remove specific line

    Kimmo Laine wrote:
    [snip] 
    yup, php 4.3.3 

    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, "$line\r\n", 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


    Ron Guest

  7. #7

    Default Re: remove specific line

    "Ron Eggler" <com> kirjoitti
    viestissä:net... 
    > yup, php 4.3.3 
    >
    > But following function doesn't remove the passed string ($email_address)
    > out of the textfile ($blacklist)[/ref]


    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

    com


    Kimmo Guest

  8. #8

    Default Re: remove specific line

    Kimmo Laine wrote:
    [snip] 
    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


    Ron Guest

  9. #9

    Default Re: remove specific line

    "Ron Eggler" <com> kirjoitti
    viestissä:net... 
    > 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.... :([/ref]


    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

    com


    Kimmo Guest

  10. #10

    Default Re: remove specific line

    [snip] 

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

    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, "$line\n", 512);
    fclose($handler);
    }
    echo $email_address." removed from ". $blacklist ."<br>\n";
    }


    Ron Guest

  11. #11

    Default Re: remove specific line

    Ron Eggler work wrote: 
    >
    > oh very bad because it doesn't work...: :( I pasted the Code below.... 
    >
    > yup, and the code looks like this:
    >
    Code:
    > //at the top of the file:
    > $blacklist="blacklist.txt";
    >
    [/ref]
    Code:
    //-------------------------------------------------------------------------- 


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


    Ron Guest

  12. #12

    Default Re: remove specific line

    "Ron Eggler work" <com> wrote in message
    news:net... 
    >>
    >> oh very bad because it doesn't work...: :( I pasted the Code below.... 
    >>
    >> yup, and the code looks like this:
    >>
    Code:
    >> //at the top of the file:
    >> $blacklist="blacklist.txt";
    >>
    [/ref]
    Code:
    > //-------------------------------------------------------------------------- 

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


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

    Change fputs($handler, "$line\n", 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


    Kimmo Guest

Similar Threads

  1. Can't seem to remove line break..
    By Roland Dalmulder in forum PHP Development
    Replies: 2
    Last Post: August 18th, 01:03 AM
  2. remove '\' with new-line
    By Jeff in forum PERL Beginners
    Replies: 3
    Last Post: March 3rd, 03:23 PM
  3. [PHP] How to remove new line character?
    By Simon in forum PHP Development
    Replies: 4
    Last Post: November 7th, 07:52 PM
  4. Remove line
    By Gunnar Hjalmarsson in forum PERL Miscellaneous
    Replies: 10
    Last Post: July 10th, 05:28 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139