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
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.
jstu...@attglobal.net
==================
> $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.
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'