Snippets

Create an account or login to be able to add, comment and rate snippets.

Navigation

Refine Tags

Snippets tagged "helper inflector" Snippets tagged "helper inflector"

Conditional Plural Helper

This one is so simple I hesitated to post it, but it's also so useful.

PluralHelper.php

<?php
 
/**
 * Returns a smart plural of the supplied word.
 * 
 * If there are more than one items in the supplied array, this function will
 * either add an 's' to the end of the supplied word, or return the optional
 * plural version.
 * 
 * @author  Kris Wallsmith <kris [dot] wallsmith [at] gmail [dot] com>
 * 
 * @param   string $text            The word you want returned
 * @param   array $array            The array of items this word is describing
 * @param   string $plural_version  An optional word to return instead of just 
 *                                  adding an 's' (i.e. "people")
 * 
 * @return  String
 */
function pluralize_if_many($text, $array, $plural_version = null)
{
    return count($array) > 1 ? ($plural_version ? $plural_version : ($text . 's')) : $text;
}
 
?>
by Kris Wallsmith on 2007-05-07, tagged helper  inflector  plural  string 
(2 comments)