This is one of those technical nitpick things. Check out the screenshot below of the new Yahoo Answers and see if you can spot the error.

The title of this thread kind of gives it away. Note the “1 Answers” should actually read “1 answer” and then when there ar 2 or more it should be pluralized. Let’s build some quick and dirty pluralizing code using PHP.
The example above seems to work great, but there is a bug in it. What if you pass this string:
The answer is it will return the wrong answer (14 answer, not plural) because only the first character is being evaluated. We need to strip out the number in the beginning first instead of using substr. Here’s the corrected code.
This will return the following:
14 Answers
144 Answers
If you would like to add commas just use the built-in number_format() function like this:
Oops, that dropped the answer, didn’t it? So we need to include in the pluralize() function like this:
function pluralize($string) {
preg_match(“|^([0-9]+)(.*)|”,$string,$matched);
if($matched[1] > 1) {
return number_format($matched[1]) . $matched[2] . ’s’;
}
return $string;
}
echo(pluralize(‘1456 Answer’));
?>
Now we’ll get 1,456 Answers pluralized.
Other solutions
Here’s another way to pluralize that requires passing a number and arguments in various languages.

Nice, but what if the string I want to pluralize is strings ending in “s” or “y?” (I personally can write that code, but you might want to point out for newbies.)
on March 17th, 2008 at 9:48 pm | #Link Comment