Help please ! How to remove lines ? ASAP plz

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
illusion123
Forum Newbie
Posts: 4
Joined: Sat Dec 09, 2006 1:13 pm

Help please ! How to remove lines ? ASAP plz

Post 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 !
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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:
illusion123
Forum Newbie
Posts: 4
Joined: Sat Dec 09, 2006 1:13 pm

Post 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...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
illusion123
Forum Newbie
Posts: 4
Joined: Sat Dec 09, 2006 1:13 pm

Post 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]
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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
}
Post Reply