Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-08-2004, 04:50 PM   PM User | #1
gazsux
New to the CF scene

 
Join Date: Jan 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
gazsux is an unknown quantity at this point
file() new line problem?

Hello,

All I am wanting to do is compare to arrays of strings and filter out one set of strings from the other. Like you may want to filter out a list of staff members names from a list of random people and staff members.

I have a staff text file, it contains:

ione
becs
glitterkat
jibbi
chelseaboy
dany
gazsux
dionysus

I have a list text file, it conatains:

ione
Camel
max
CIACodeName
Disturbed
Explicit
frekka
Frekkeh
gazsux
Habbex
Investigations.
NewsContact
pete

I have the code:

PHP Code:
<?php

$file
=file("file/list.txt");
$staff=file("file/staff.txt");

$result array_diff($file$staff);
foreach(
$result as $name) {
echo 
"$name<br>";
}

?>
However, the result I get is:

ione
Camel
max
CIACodeName
Disturbed
Explicit
frekka
Frekkeh
gazsux
Habbex
Investigations.
NewsContact
pete

I cant figure out why! is it to do with \n chars being bought in using file()? How can I make it just list non-staff names. The desired result is:

Camel
max
CIACodeName
Disturbed
Explicit
frekka
Frekkeh
Habbex
Investigations.
NewsContact
pete

I would appreciate any help, cheers
gazsux is offline   Reply With Quote
Old 01-08-2004, 05:16 PM   PM User | #2
mordred
Senior Coder


 
Join Date: Jun 2002
Location: frankfurt, german banana republic
Posts: 1,848
Thanks: 0
Thanked 0 Times in 0 Posts
mordred is an unknown quantity at this point
If I change the order of the parameters used in array_diff() of your sample code, I do get your desired results.

$result = array_diff($staff, $file);
__________________
De gustibus non est disputandum.
mordred is offline   Reply With Quote
Old 01-08-2004, 09:08 PM   PM User | #3
gazsux
New to the CF scene

 
Join Date: Jan 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
gazsux is an unknown quantity at this point
If I do that then I get the opposite of what I want, I get:

ione
becs
glitterkat
jibbi
chelseaboy
dany
gazsux
dionysus

They are the staff names I want to be removed.

However, I tried using array_intersect() to do the opposite and that doesn't work either...
gazsux is offline   Reply With Quote
Old 01-08-2004, 11:03 PM   PM User | #4
mordred
Senior Coder


 
Join Date: Jun 2002
Location: frankfurt, german banana republic
Posts: 1,848
Thanks: 0
Thanked 0 Times in 0 Posts
mordred is an unknown quantity at this point
Looks like you're right with your assumptions that newlines (maybe of different filesystems?) are screwing up array_diff(). I don't know what happened with my first try, maybe I named the files incorrectly. Anyway, with PHP 4.3.4 on w2k I get your desired result after applying trim() on each line:

PHP Code:
$list array_map("clean"file("list.txt"));
$staff array_map("clean"file("staff.txt"));

function 
clean($value) {
    return 
trim($value);
}
$result array_diff($list$staff);

foreach(
$result as $name) {
    print 
"$name\n";

hth
__________________
De gustibus non est disputandum.
mordred is offline   Reply With Quote
Old 01-08-2004, 11:20 PM   PM User | #5
gazsux
New to the CF scene

 
Join Date: Jan 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
gazsux is an unknown quantity at this point
Top man!

Cheers for the help mate, it works!

I think I will look into alternatives to file() I have had a few probs with it before relating to \n.
gazsux is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:17 PM.


Advertisement
Log in to turn off these ads.