tags:

views:

74

answers:

3

Does php have any function to deal with pluralising or depluralising words?

Obviously it's pretty easy to take off or replace the 's' at the end of words like 'apple' but other words are not so simple.

If php doesn't have a native way of dealing with it, how do other languages deal with the problem? Is there a function that can handle pluralisation/depluralisation or is the english language just too gramatically inconsistent?

A: 

I think you should check this sample out:

http://kuwamoto.org/2007/12/14/how-to-pluralize-in-php-and-please-help-me-check-the-code/

There might be some bugs and I haven't tested it, but it should be enough to get you started.

phidah
+1  A: 

I am normally using PHP only from within frameworks that implements their own way to deal with this problem, yet I think the answer to your question might be gettext.

Gettext is the GNU internationalization and localization (i18n) library. It has been developed as a tool for i18n and l10n (internationalisation and localisation) but - exactly because of this, it has plenty of functions to deal with plural forms, given that other languages might have much more complex ways of forming plurals than English does.

PHP implments gettext so that might be a good starting point for you to look into. Finally, you might wish to have a look to this other answer, suggesting an alternative way of doing this.

EDIT: I think ngettext() might be the function you are looking for.

mac
+1  A: 

Check out Damien Conway's paper on this. It illustrates nicely what a thorny topic this is in English alone.

How do other languages deal with this (as you've asked) ? Perl has a module for this (written by Damien) which encodes the above paper. See Lingua-EN-Inflect. I don't know of any modules in other languages which do quite this.

Brian Agnew