Inflect is a memoizing inflector for PHP — pluralize, singularize, and format counted nouns for English.
- PHP 8.1+ (the legacy PHP 5.3 baseline is maintained on the
php5.3branch for back-ports)
composer require mmucklo/inflectuse Inflect\Inflect;
Inflect::pluralize('test'); // 'tests'
Inflect::singularize('tests'); // 'test'
Inflect::pluralizeIf(1, 'cat'); // '1 cat'
Inflect::pluralizeIf(3, 'person'); // '3 people'Irregulars, uncountables, and case are handled automatically:
Inflect::pluralize('Man'); // 'Men'
Inflect::pluralize('datum'); // 'data'
Inflect::pluralize('news'); // 'news'
Inflect::singularize('criteria'); // 'criterion'
Inflect::singularize('Children'); // 'Child'
// Double-inflection is a no-op:
Inflect::pluralize('people'); // 'people'
Inflect::singularize('datum'); // 'datum'Results are memoized in a process-local static cache.
Returns the plural form of the given word. Empty input returns ''.
Returns the singular form of the given word. Empty input returns ''.
Returns "$count $word" with the word pluralized when $count !== 1.
Passing null to any of these methods raises a TypeError — callers should handle null explicitly.
Semantic versioning. The currently supported line is 2.x (PHP 8.1+).
See CHANGELOG.md for release history and ROADMAP.md for planned work (locale support, extension APIs, etc.).
2.0.0 is a breaking release. Highlights:
- PHP 8.1+ required (was 5.3.17+).
src/Inflect/Inflect.phpmoved tosrc/Inflect.php(PSR-0 → PSR-4).Inflectisfinal; rule tables areprivate.pluralize(null)/singularize(null)now throwTypeError.phpunit.xml-distrenamed tophpunit.xml.dist(PHPUnit 10 convention).
Full list in the v2.0.0 release notes.
See CONTRIBUTING.md for dev setup, running the test / static analysis / style suite, and PR conventions.
Originally forked from Sho Kuwamoto's improved pluralizer. Many thanks to Sho, and to the Ruby on Rails and Doctrine inflector projects from which rule patterns continue to be borrowed.
MIT — see LICENSE.
