How to I pluralize in php?
Ok so i have this chunk of code
<p id="number_of_stations" class="text left"><?php $_GET['count'] != "" ? print "{$_GET['count']}开发者_Python百科 Stations" : print "Number of Stations" ?></p>
and $_GET['count'] will be either 1, 2, 3, 4 or 5 and i need to print out the following
1 Station
2 Stations
3 Stations
4 Stations
5 Stations
but the code i have above will always print the plural form and not the singular
Simplified:
echo "$count Station" . ($count == 1 ? null : 's');
You may want to create a function for this if you're using this often. If you're looking for automagic pluralization of any word you'll need to write an Inflector.
On this simple case you can just add an s
at the end (see deceze's answer), but if you want a true pluralize()
method that can handle irregular forms (eg: 1 box, 2 boxes; 1 person, 2 people, etc) then there's nothing built-in.
You'll have to write your own class/method, or use one available on the Internets.
Here's an implementation: http://blog.eval.ca/2007/03/03/php-pluralize-method/
精彩评论