Page 1 of 1

Help please ! How to remove lines ? ASAP plz

Posted: Sat Dec 09, 2006 1:17 pm
by illusion123
Hello.

I have 2 files.
In first file I have lines with email addresses of subscribers.
In second file I have lines with emails who want to unsubscribe.

How to remove from the first file lines that I have in the second (unsubscribe) file ?
Please tell me how to make this with PHP or linux shell.

Thank you very much !

Posted: Sat Dec 09, 2006 1:24 pm
by jayshields
Do it by hand?

It can be done in PHP, but the only way I can think of uses

Code: Select all

str_replace()
in a nested for loop which would be very slow.

pseudo code:

Code: Select all

foreach line in unsubscribe list as linex
  foreach line in subscribe list as liney
    if linex == liney
      delete liney
    end if
  end foreach
end foreach

Posted: Sat Dec 09, 2006 1:25 pm
by John Cartwright
1. use file() on both of the files
2. loop through your second file using foreach()
3. use in_array() against the first array of the looped value to double check they are in first file
4. unset() the value of the looped array against the first file
5. rewrite the first file using fopen() and fwrite()

Enjoy.

jayshields: No need for nested loops :wink:

Posted: Sat Dec 09, 2006 1:40 pm
by illusion123
Please give me a php code. Please =(

<?php
$unsubscribe = "un.txt";
$subscribe = "subscribers.txt";

foreach line in $unsubscribe list as linex
foreach line in $subscribe list as liney
if linex == liney
delete liney
end if
end foreach
end foreach
>

Don`t work.
Help please...

Posted: Sat Dec 09, 2006 1:47 pm
by John Cartwright
Please give me a php code. Please =(
If you want to hire me, then I will. Until then, try it for yourself, and when you get stuck we'll help. Were not here to do the work for you, but help you learn.

Posted: Sat Dec 09, 2006 2:17 pm
by illusion123
Jcart | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


<?php

Code: Select all

$subscribers = file('subscribers.txt');
$unsubscribers = file('un.txt');
$sortedsubscribers = "sortedsubscribers.txt";

foreach ($subscribers as &$value) {

foreach ($unsubscribers as &$unvalue) {

if ($value = $unvalue) then ($value="")
$fh = fopen($sortedsubscribers, 'a') or die("Error!!");
fwrite($fh, "$value\n");
fclose($fh);

}
}

>
Don`t work =(
Run the following in a new file and tell us the results please.


Jcart | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sat Dec 09, 2006 3:23 pm
by jayshields
Try using the comparison operator == instead of the assignment operator =

Also, try using curly braces to encapsulate the statements you want to be associated with your if(), like this:

Code: Select all

if(/*condition*/)
{
  //statement 1
  //statement 2
}