Click here to get back home

how to make a filter function-filter out items in array?

 HomeNewsGroups | Search

comp.lang.php - PHP programming language discussions 

get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
how to make a filter function-filter out items in array? J. Frank Parnell 11-04-2009
Posted by J. Frank Parnell on November 4, 2009, 6:02 pm
Please log in for more thread options


tried this, but didnt work for more than one $delete. I understand
why, but not how to fix.

function arrayfilterout ($input, $delete) {
foreach($input as $i){
        foreach($delete as $d){
                if(strpos($i, $d)===false){
                        $newra[] = $i;//doesnt work here
                }
        }
}
return $newra;
}

$files= array('file.jpg','file.gif','file.doc','file.wmv');
$delete= array ('doc','wmv');

$pics = arrayfilterout ($files, $delete);

I want $pics to be jpgs and gifs only. Please dont take this example
literally, i need the strpos type filter.

thanks
J



Posted by Jerry Stuckle on November 4, 2009, 7:15 pm
Please log in for more thread options


J. Frank Parnell wrote:
show/hide quoted text

The way you have it written, it will add the file to the new array if
ANY of the tests are false. What you want is to add the file only if
ALL of the tests are false. One way:

function arrayfilterout ($input, $delete) {
$newra = array(); // Initialize the array!
foreach($input as $i){
$add = true; // Assume the file will be added
foreach($delete as $d){
        if(strpos($i, $d)===true){
$add = false; // Match - do not add
break; // Match found, no further need to loop
}
}
if ($add) { // If no match was found
$newra[] = $input;
}
}
return $newra;
}

WARNING! DO NOT TRUST THE FILE EXTENSION IF IT IS A USER-UPLOADED FILE!

This also has other problems - like what happens if your file name is
something like 'mydoc.jpg'? Or 'my.doc.jpg'? Both will be incorrectly
filtered out.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Posted by crimaniak@googlemail.com on November 4, 2009, 8:43 pm
Please log in for more thread options


e:

show/hide quoted text
example
show/hide quoted text
You need array_filter() and preg_match() functions. And think about
cases like 'jpg_list.doc' or 'doc.jpg.wmv'.
I think you need mask like '/\.('.join('|',$delete).')$/i'

Posted by J. Frank Parnell on November 6, 2009, 5:32 pm
Please log in for more thread options


On Nov 4, 5:43=C2=A0pm, "criman...@googlemail.com"
ote:
show/hide quoted text
s example
show/hide quoted text

Great, thanks guys :)

Similar ThreadsPosted
get first and last items in array November 15, 2007, 6:15 pm
How to reference these array items May 6, 2009, 8:06 am
Printing array items sent in a form February 2, 2007, 9:52 am
Check if any items in an array (php) is in a table (mysql). April 17, 2006, 10:59 am
Traversing $_REQUEST array and excluding items May 30, 2007, 1:43 am
list box with items for deletion passed from php array December 25, 2008, 4:14 pm
Counting number of similar items in an array December 31, 2009, 2:24 pm
Make selection in array January 9, 2006, 4:31 am
Can I make an array of tuples? Is there a better way? August 14, 2007, 9:16 pm
how do you make a string of binary numbers from an array August 11, 2006, 4:34 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Driving a better car - Fuelzilla.com

Cabling site for homeowners and pros alike - Cabling-Design.com

Friends:

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap
Privacy Policy