|
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
> 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
>
>
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
> $files=3D array('file.jpg','file.gif','file.doc','file.wmv');
> $delete=3D array ('doc','wmv');
> $pics =3D arrayfilterout ($files, $delete);
> I want $pics to be jpgs and gifs =C2=A0only. =C2=A0Please dont take this =
example
show/hide quoted text
> 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'
|
|
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
> > $files=3D array('file.jpg','file.gif','file.doc','file.wmv');
> > $delete=3D array ('doc','wmv');
> > $pics =3D arrayfilterout ($files, $delete);
> > I want $pics to be jpgs and gifs =C2=A0only. =C2=A0Please dont take thi=
s example
show/hide quoted text
> > literally, i need the strpos type filter.
> =C2=A0You 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'
Great, thanks guys :)
|
| Similar Threads | Posted | | 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 |
|
> 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
>
>