Item randomizer PHP script

Finished in the Autumn, 2002. Unlikely to be further develped by me.

A script for getting a random item in an array or a random filename in a directory

return to code & design index

<?php
/* * * * * * * * * * * * * * * *
*
* String / array / filename randomizer function
* Copyright 2002 Olli Savolainen
* You may use, modify and distribute this script freely as long as you 
* don't remove any comments.
* version 1.0 [requires PHP 4; uses, for example, the function 
* array_rand()]
*
* Syntax:
*
* randItem ([NUMBER_OF_RESULTS],[ARRAY_OF_ITEMS],
* [DIRECTORY_FOR_FILES], [ACCEPTED_EXTENSIONS]);
*
* All arguments are optional. If no arguments are specified, echoes a random filename in the current directory to the browser
*
* NUMBER_OF_RESULTS  (optional)
If set to 0 (zero), the function echoes nothing but returns one random item.
If set to 1 or greater, the function echoes that number of entries to the browser, separated by '\n' (a newline)

* ARRAY_OF_ITEMS (optional)
If specified, the function will use this one-dimensional array as the material for the shuffle

* DIRECTORY_FOR_FILES (optional)
The directory specified will be used as the directory instead of the current directory ("."), which is the default

* ACCEPTED_EXTENSIONS (optional)
The extensions which are accepted for the shuffle. A single string (for example: "gif") or an array containing multiple extensions [for example: array("gif", "jpeg")] By default, all extensions are accepted.

* Examples
Return one of the GIF and JPEG image filenames in the current directory
$extensions=array("gif", "jpg", "jpeg");
randItem(0,'','',$extensions);

Return a colour
randItem(0,array("pink", "red", "cyan", "maroon"));

Echo two of the GIF and JPEG image filenames in the current directory, separated by a newline, to the browser
randItem(2,'','',$extensions);

Echo a random filename in the directory "../f00bar/" to the browser
randItem('','','../f00bar');


* * * * * * * * * * * * * * * */

function randItem($number=''$items=''$dir=''$extension='') {
    if ( (
substr($dir, -1) != "/") && (!(empty($dir))) ){
        
$dir.="/";
    }
    if (empty(
$number)&&(!($number===0)))
    {
        
$number=1;  
        
// if there's nothing, not even a zero in there, set the default value
    
}
    if (!
is_array($items)) 
        
$items fileArray($dir$extension); 
        
// if the user didn't give an array to use, get an array from the function
        // parameters given to randItem() are passed to fileArray()

    
srand((double)microtime()*1000000);
    
    if (
count($items)>1){
        
$rand_keys array_rand ($items,count($items));
        
// array_rand: since PHP 5.2.10 the resulting array of keys is no longer shuffled, so do it manually:
        
shuffle($rand_keys);
        
// returns an array with array indexes in random order
    
}
    else{
        
$rand_keys = array(0); 
        
// if there is only one item in the array it need not be shuffled.
    
}

    if (
$number==0)
        return 
$items[$rand_keys[0]]; 
    else{ 
// go through the items
        
if ($number>count($items)) $number=count($items);
        for (
$i=0;$i<$number;$i++){
            if (
$i>0){
                echo 
"\n";
            }
            echo 
$items[$rand_keys[$i]];
        }
        }
}


function 
fileArray($dir=''$extension=''){
    
$files=array();
    if (!empty(
$dir)) 
        
$dirhandle opendir($dir);
    else
        
$dirhandle opendir("./");
    
$i=0;
    while(
$file readdir($dirhandle)){ 

        
$filearray=explode(".",$file);
        if ((
$file!="..")&&($file!=".")){

            if (
is_string($extension)){
                
                if (
strtolower($filearray[1])==strtolower($extension)){
                    
$files[$i] = $dir.$file;
                    
$i++;
                    }
            }
            
            elseif (
is_array($extension)){

                    foreach(
$extension as $ext){
                        if (
strtolower($filearray[1])==strtolower($ext))
                            {
                            
$files[$i] = $dir.$file;
                            
$i++;
                            }
                        }
                }

            else{ 
//if no extensions were specified, accept all of them
                            
                
$files[$i] = $dir.$file;
                
$i++;

            }
            
        }
        
    }
    return 
$files;
}





?>

return to code & design index