首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 實現英文名詞複數化工具函數

     編輯:PHP 相關知識

PHP 實現英文名詞複數化工具函數

在開發多語言系統或動態生成文案時,常需根據數量自動輸出單複數形式。英文名詞複數規則繁雜(規則變化 + 大量不規則詞),手寫邏輯極易出錯。本文提供一個開箱即用的 PHP 函數 `_plurals_format()`,內建超過 120 個不規則複數映射,並支援規則性複數自動加 `s`。

函數特性

  • 支援 不規則複數(如 child → children, mouse → mice, datum → data)。
  • 支援 同形單複數(如 sheep, fish, series, species)。
  • 支援 規則複數:數量 > 1 時自動加 `s`(如 biscuit → biscuits)。
  • 參數檢查:空字串、非數字、數量 ≤ 0 均回傳空字串。

完整代碼

<?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.
?>

擴充建議

  1. 整合 i18n 庫:若專案已使用 Symfony Translation、Laravel Lang 或 gettext,建議改用標準複數化公式(ICU MessageFormat),支援更多語言的複數規則(如斯拉夫語族 3 種複數形式)。
  2. 快取映射表:將 `$arPluralName` 提升為靜態變數或類常量,避免重複建立陣列。
  3. 單元測試:針對邊界值(0, 1, 2)、不規則詞、同形詞編寫測試案例,確保重構不破壞功能。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved