Skip to content

Instantly share code, notes, and snippets.

@mcfdn
Created October 7, 2012 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcfdn/3848540 to your computer and use it in GitHub Desktop.
Save mcfdn/3848540 to your computer and use it in GitHub Desktop.
Manage Singular and Plural Words
<?php
/**
* @author James McFadden <james@jamesmcfadden.co.uk>
*/
final class PluralHelper
{
private static $irregularPluralForms = array(
'alumnus' => 'alumni',
'analysis' => 'analyses',
'appendix' => 'appendices',
'axis' => 'axes',
'barracks' => 'barracks',
'cactus' => 'cacti',
'child' => 'children',
'criterion' => 'criteria',
'deer' => 'deer',
'echo' => 'echoes',
'elf' => 'elves',
'embargo' => 'embargoes',
'focus' => 'foci',
'fungus' => 'fungi',
'goose' => 'geese',
'hero' => 'heroes',
'hoof' => 'hooves',
'index' => 'indices',
'knife' => 'knives',
'leaf' => 'leaves',
'life' => 'lives',
'man' => 'men',
'mouse' => 'mice',
'nucleus' => 'nuclei',
'person' => 'people',
'phenomenon' => 'phenomena',
'potato' => 'potatoes',
'radius' => 'radii',
'self' => 'selves',
'stimulus' => 'stimuli',
'syllabus' => 'syllabi',
'tomato' => 'tomatoes',
'torpedo' => 'torpedoes',
'veto' => 'vetoes',
'woman' => 'women'
);
/**
* Work out whether a word should be plural or singular
*
* This is based on the count given, and checks against
* some irregular plural forms
*
* @param int $count
* @param string $word
* @return string
*/
public static function managePlural($count, $word)
{
$lcWord = trim(strtolower($word));
if($count == 1) {
if(in_array($lcWord, self::$irregularPluralForms)) {
return $count . ' ' . array_search($word, self::$irregularPluralForms);
}
return $count . ' ' . $word;
} else {
if(array_key_exists($lcWord, self::$irregularPluralForms)) {
return $count . ' ' . self::$irregularPluralForms[$lcWord];
} else if(in_array($lcWord, self::$irregularPluralForms)) {
return $count . ' ' . $word;
}
return $count . ' ' . $word . 's';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment