* Thus wrote PHP Gen:
>...
> 
> Looking in the manual I have tried sort() without any
> luck, then looking further I found natcasesort() which
> would be perfect for my needs right now, but cant get
> it to work :-( 
> 
> Below is the function (its not big)
> 
> *********** Start function ********************
> 
> function directory($dir,$filters){
>       $handle=opendir($dir);
>       $files=array();
>       if ($filters == "all"){while(($file =

For starters, if your going to provide some code, make sure its
readable by others.. trying to figure out what it is doing is
nearly impossible the way it is written..

function directory($dir,$filters){

  $handle=opendir($dir);
  $files=array();

  if ($filters == "all"){

    while(($file = readdir($handle)) !== false) { 
      $files[] = $file;
    }

  }

  if ($filters != "all") {
    $filters=explode(",",$filters);

    while (($file = readdir($handle)) !== false) {

      for ($f=0;$f<sizeof($filters);$f++) {
        $system=explode(".",$file);

        if ($system[1] == $filters[$f]) {
          $files[] =+ $file;
        }

      }

    }
  }

  closedir($handle);

  return $files;
}

ahh.. much better, but before we go on.. some redefining of the
function: 

/* if no filter provided, this will
 * return all files in the directory */
function directory($dir,$filters=''){

  $handle=opendir($dir);
  if (! $handle ) { /* better check this!! */
    return false;
  }

  $files=array();

  /* I may have extension I want that ends in .all 
   * function is redefined to reflect this */

  //if ($filters == "all")
  if (! $filters){

    while(($file = readdir($handle)) !== false) { 
      $files[] = $file;
    }

  } else {
    /* Don't need to re-evaluate $filters since this
     * is an if else situation. */

    //$filters=explode(",",$filters);
    /* not wise to change the meaning of a variable */

    $filter_list = explode(',', $filters);

    while (($file = readdir($handle)) !== false) {

      /* ouch..!  this is rather expensive, in a tight loop
       * always try to have the php engine do the filtering
       * in situations like this. see after /intensive code..
       
      for ($f=0;$f<sizeof($filters);$f++) {
        $system=explode(".",$file);

        // note: this is assuming that there is only one dot
        if ($system[1] == $filters[$f]) {

          // I'm assuming =+ was by accident, should've been =
          $files[] =+ $file;

        }
      }
      // end intensive code
      */

      // instead... simple extension finder
      if ($dot = strrpos($file, '.') !== false) {

        // and see if it passes the filter
        if (in_array(substr($file, $dot+1), $filter_list)) {
          $files[] = $file;
        }
        // note: preg_match'ing a pattern would probably be most
        // optimal, since the pattern is cached.
      }

    }
  }

  closedir($handle);

  return $files;
}

> $pics=directory("pics","jpg,JPG,JPEG,jpeg,png,PNG");
> 
> *********** End function ********************

> 
> I have tried putting natcasesort() in many places but
> not working....

oh. well.. how were you trying natcasesort()?

With the example provided in the manual:

$array1 = $array2 = array('IMG0.png', 'img12.png', 'img10.png',
'img2.png', 'img1.png', 'IMG3.png');

//...

natcasesort($array2);
print_r($array2);

should help.


Curt
-- 
First, let me assure you that this is not one of those shady pyramid schemes
you've been hearing about.  No, sir.  Our model is the trapezoid!

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to