<?php
/**
 * Create English List
 *
 * Converts arrays like (1,2,3) to a list in English like: "1, 2 and 3"
 * If only two array elements exist it returns: "1 and 2"
 * If only one array element exists it returns: "1"
 * If zero array element exists it returns: ""
 * If an array element is an empty string, it is skipped.
 *
 * @param array $arr The array to convert.
 * @param bool $oxford Option to put a comma before 'and'. Default False.
 * @param bool $add_quotes Option to show quotes around elems. Default False.
 * @return string Return an English list or empty string if the array is empty.
 * @author Ryan Kulla <rkulla@gmail.com>
 * @copyright Copyright (c) 2008-2009, Ryan Kulla
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 */
function create_english_list($arr$oxford=false$add_quotes=false)
{
    
// Format string to use if there's only one array element.
    
$format_str1 '%s';       

    
// Format string to use if there's more than one array element.
    
if ($oxford) {
        
$format_str2 '%s, and %s';
    } else {
        
$format_str2 '%s and %s';
    }

    
// Optionally put double-quotes around each array element.
    
if ($add_quotes) {
        
$arr array_map(create_function('$val''return "\"$val\"";'), $arr);
    }

    
// Filter out strings that don't contain at least one letter or number.
    
$arr array_filter($arrcreate_function('$val',
                        
'return preg_replace("/[^a-zA-Z0-9]/", "", $val);'));

    
// Store the total number of elements that exist in our array.
    
$nelems count($arr);

    
// If the array is empty, return an empty string.
    
if ($nelems == 0) {
        return 
'';
    } else {
        
$last_elem array_pop($arr);
        return 
vsprintf(/* Format string: */
                        
$nelems <= $format_str1 $format_str2
                        
/* Arguments: */
                        
$nelems <= $last_elem : array(implode($arr', '),
                                                          
$last_elem)
                       );
    }
}