<?php
function pluralize(string $word, int $n = 2): string {
if ($n === 1) return $word;
static $irregular = [
'man'=>'men','woman'=>'women','child'=>'children','person'=>'people',
'tooth'=>'teeth','foot'=>'feet','mouse'=>'mice','goose'=>'geese',
];
$low = strtolower($word);
if (isset($irregular[$low])) return $irregular[$low];
$last = substr($low, -1);
$last2 = substr($low, -2);
if (in_array($last2, ['ch','sh','ss','us','is'], true)) return $word . 'es';
if ($last === 'y' && !in_array(substr($low, -2, 1), ['a','e','i','o','u'], true)) {
return substr($word, 0, -1) . 'ies';
}
if ($last === 'f') return substr($word, 0, -1) . 'ves';
return $word . 's';
}
echo pluralize('book', 3); // books
echo pluralize('child', 2); // children
echo pluralize('city', 5); // cities
echo pluralize('item', 1); // item
Create a free account and build your private vault. Share publicly whenever you want.