在開發多語言系統或動態生成文案時,常需根據數量自動輸出單複數形式。英文名詞複數規則繁雜(規則變化 + 大量不規則詞),手寫邏輯極易出錯。本文提供一個開箱即用的 PHP 函數 `_plurals_format()`,內建超過 120 個不規則複數映射,並支援規則性複數自動加 `s`。
<?php
if (!function_exists('_plurals_format')) {
/**
* 正確輸出英文名詞複數形式
* @param string $sPluralName 單數名詞
* @param int $iAmount 數量
* @return string 複數形式字串
*/
function _plurals_format($sPluralName, $iAmount)
{
if (!$sPluralName || !is_numeric($iAmount) || $iAmount <= 0) {
return '';
}
// 不規則複數映射表(可按需擴充)
$arPluralName = [
'addendum' => 'addenda', 'alga' => 'algae',
'alumna' => 'alumnae', 'alumnus' => 'alumni',
'analysis' => 'analyses', 'antenna' => 'antennas', // antennae 亦可
'apparatus'=> 'apparatuses', 'appendix' => 'appendices', // appendixes 亦可
'axis' => 'axes', 'bacillus' => 'bacilli',
'bacterium'=> 'bacteria', 'basis' => 'bases',
'beau' => 'beaux', 'bison' => 'bison',
'buffalo' => 'buffalos', 'bureau' => 'bureaus',
'bus' => 'busses', 'cactus' => 'cactuses', // cacti 亦可
'calf' => 'calves', 'child' => 'children',
'corps' => 'corps', 'corpus' => 'corpora', // corpuses 亦可
'crisis' => 'crises', 'criterion' => 'criteria',
'curriculum'=> 'curricula', 'datum' => 'data',
'deer' => 'deer', 'die' => 'dice',
'dwarf' => 'dwarfs', 'diagnosis' => 'diagnoses',
'echo' => 'echoes', 'elf' => 'elves',
'ellipsis' => 'ellipses', 'embargo' => 'embargoes',
'emphasis' => 'emphases', 'erratum' => 'errata',
'fireman' => 'firemen', 'fish' => 'fish', // fishes 亦可
'focus' => 'focuses', 'foot' => 'feet',
'formula' => 'formulas', 'fungus' => 'fungi', // funguses 亦可
'genus' => 'genera', 'goose' => 'geese',
'half' => 'halves', 'hero' => 'heroes',
'hippopotamus'=> 'hippopotami','hoof' => 'hoofs', // hooves 亦可
'hypothesis'=> 'hypotheses', 'index' => 'indices', // indexes 亦可
'knife' => 'knives', 'leaf' => 'leaves',
'life' => 'lives', 'loaf' => 'loaves',
'louse' => 'lice', 'man' => 'men',
'matrix' => 'matrices', 'means' => 'means',
'medium' => 'media', 'memorandum' => 'memoranda',
'millennium'=> 'millenniums','moose' => 'moose',
'mosquito' => 'mosquitoes', 'mouse' => 'mice',
'nebula' => 'nebulae', 'neurosis' => 'neuroses',
'nucleus' => 'nuclei', 'oasis' => 'oases',
'octopus' => 'octopi', 'ovum' => 'ova',
'ox' => 'oxen', 'paralysis' => 'paralyses',
'parenthesis'=> 'parentheses','person' => 'people',
'phenomenon'=> 'phenomena', 'potato' => 'potatoes',
'radius' => 'radii', 'scarf' => 'scarfs', // scarves 亦可
'self' => 'selves', 'series' => 'series',
'sheep' => 'sheep', 'shelf' => 'shelves',
'scissors' => 'scissors', 'species' => 'species',
'stimulus' => 'stimuli', 'stratum' => 'strata',
'syllabus' => 'syllabi', 'symposium' => 'symposia', // symposiums 亦可
'synthesis'=> 'syntheses', 'synopsis' => 'synopses',
'tableau' => 'tableaux', 'that' => 'those',
'thesis' => 'theses', 'thief' => 'thieves',
'this' => 'these', 'tomato' => 'tomatoes',
'tooth' => 'teeth', 'torpedo' => 'torpedoes',
'vertebra' => 'vertebrae', 'veto' => 'vetoes',
'vita' => 'vitae', 'watch' => 'watches',
'wife' => 'wives', 'wolf' => 'wolves',
'woman' => 'women', 'zero' => 'zeros', // zeroes 亦可
];
// 數量為 1 直接回傳原形
if ($iAmount == 1) {
return $sPluralName;
}
// 命中不規則映射
if (isset($arPluralName[$sPluralName])) {
return $arPluralName[$sPluralName];
}
// 規則複數:直接加 s
return $sPluralName . 's';
}
}
// 使用範例
echo 'Sybil ate three ' . _plurals_format('biscuit', 3) . ', one after the other.' . PHP_EOL;
// 輸出: Sybil ate three biscuits, one after the other.
echo 'The two ' . _plurals_format('woman', 2) . ' will meet tomorrow in the final.' . PHP_EOL;
// 輸出: The two women will meet tomorrow in the final.
?>