As you can see, it does the job, but it’s really clumsy. Can anyone suggest a better solution? One possibility is to come up with a constant value - e.g. $LastLetters; - that can contain varying numbers of characters, so I could make a switch that looks like this:
switch($LastLetters)
{
case 'mouse':
$Plural = 'mice';
break;
case 'fox':
$Plural = 'foxes';
break;
default:
break;
}
I don’t understand why you are comparing last n number of characters instead of the whole species name?
To get the plural I would use a pre-defined array of irregular names and if a name is not on the list then use a generic method for making a plural, e.g.:
This is a very basic example, you might improve the auto-generation script to be able to detect the ending x, ch, sh, etc. and properly append es to the name and so you wouldn’t need to include such nouns in the irregular list. You might also load the irregular list from a db so that it can be updated easily in an admin panel, etc.
Thanks for the tip; I’ll experiment with that. However, I’m not sure if that will work because of the extraordinary number of names I’m working with.
For example, there’s no animal called a “mouse.” Rather, there are hundreds of different species with names that END in mouse - house mouse, field mouse, northern meadow mouse, common dormouse, etc.
On the other hand, I might be able to combine your script with my original idea, making an array of irregular endings, rather than irregular names.
BTW, you said it’s a database-driven site, if that is so then I presume you have a table with species. Then it might be easiest to add another field plural_name that would be required for irregular nouns and would be entered into the db by whoever does this. If plural_name is not NULL then it is used for plural, if it’s NULL then the plural is created automatically by your php script. In this way you wouldn’t need to maintain a separate list of irregular names.
Wow, thanks for the code. There’s a lot for me to learn there.
That was my original intention, and I might still wind up doing it. However, I’m afraid my page-loading time will suffer if I join too many tables. I thought I might be able to put together a relatively simple PHP script that does the job with less overhead. First, I eliminate the majority of species by establishing a default: Simply add “S” to make the name plural.
Then I eliminate some of the larger groups of exceptions -
any name that ends in y (except kouprey) = change “y” to “ies”
mouse = mice
fox = foxes
etc.
Then I can look at the remaining names and try to figure out some script that conveniently handles them.
One thing that will make it more complex is if I include preferences. For example, some animal names have two plural forms, like “antelope” and “antelopes.” In some cases, the singular form (antelope) is preferred, while name+s is preferred in other cases.
So I may have to do this in a database to avoid getting bogged down in all the details.
Either way, I’ll get some use out of your code. It looks really cool. Thanks!
Page loading time will not suffer - the idea is not to create another table but simply add one more field to the existing table with species. I can guarantee you will not notice the time overhead of a single field, you will even find it hard to measure (!) and will certainly be faster than dealing with irregular nouns in pure php. For example:
// suppose $species contains data from a single row from the 'species' table
if ($species['plural_name'] !== null) {
$plural = $species['plural_name'];
} else {
// auto plural
$plural = $species['name'] . "s";
}
This is equivalent of leaving the plural_name field NULL in the db. You might automate initial filling of the field like this if you have many records already:
UPDATE species SET plural_name=REPLACE(name, 'mouse', 'mice') WHERE name='mouse' OR name LIKE '% mouse';
Yes, that would make things more complex but I think the easiest way then would be to have two db fields for the irregular plural, each being used in its preferred context. Of course, I’d make them both optional (NULLable).
Glad to be of help, I think you have enough ideas now to figure out the best approach