Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Function: pluralize
- * Returns a pluralized string. This is a port of Rails' pluralizer.
- *
- * Parameters:
- * $string - The string to pluralize.
- */
- function pluralize($string) {
- global $pluralizations;
- if (in_array($string, array_keys($pluralizations)))
- return $pluralizations[$string];
- else {
- $uncountable = array("moose", "sheep", "fish", "series", "species", "rice", "money", "information", "equipment", "piss");
- $replacements = array("/person/i" => "people",
- "/man/i" => "men",
- "/child/i" => "children",
- "/cow/i" => "kine",
- "/goose/i" => "geese",
- "/(ax|test)is$/i" => "\\1es",
- "/(octop|vir)us$/i" => "\\1ii",
- "/(alias|status)$/i" => "\\1es",
- "/(bu)s$/i" => "\\1ses",
- "/(buffal|tomat)o$/i" => "\\1oes",
- "/([ti])um$/i" => "\\1a",
- "/sis$/i" => "ses",
- "/(hive)$/i" => "\\1s",
- "/([^aeiouy]|qu)y$/i" => "\\1ies",
- "/^(ox)$/i" => "\\1en",
- "/(matr|vert|ind)(?:ix|ex)$/i" => "\\1ices",
- "/(x|ch|ss|sh)$/i" => "\\1es",
- "/([m|l])ouse$/i" => "\\1ice",
- "/(quiz)$/i" => "\\1zes");
- $replaced = $string;
- foreach ($replacements as $key => $val) {
- if (in_array($string, $uncountable))
- break;
- $replaced = preg_replace($key, $val, $string);
- if ($replaced != $string)
- break;
- }
- if ($replaced == $string and !in_array($string, $uncountable))
- return $string."s";
- else
- return $replaced;
- }
- }
- /**
- * Function: depluralize
- * Look in $pluralizations for a depluralization of $string.
- *
- * Parameters:
- * $string - The string to depluralize.
- */
- function depluralize($string) {
- global $pluralizations;
- $copy = $pluralizations;
- unset($copy["feathers"]);
- $reversed = array_flip($copy);
- if (isset($reversed[$string]))
- return $reversed[$string];
- else {
- $uncountable = array("moose", "sheep", "fish", "series", "species", "rice", "money", "information", "equipment", "piss");
- $replacements = array("/people/i" => "person",
- "/^men/i" => "man",
- "/children/i" => "child",
- "/kine/i" => "cow",
- "/geese/i" => "goose",
- "/(ax|test)es$/i" => "\\1is",
- "/(octop|vir)ii$/i" => "\\1us",
- "/(alias|status)es$/i" => "\\1",
- "/(bu)ses$/i" => "\\1s",
- "/(buffal|tomat)oes$/i" => "\\1o",
- "/([ti])a$/i" => "\\1um",
- "/ses$/i" => "sis",
- "/(hive)s$/i" => "\\1",
- "/([^aeiouy]|qu)ies$/i" => "\\1y",
- "/^(ox)en$/i" => "\\1",
- "/(vert|ind)ices$/i" => "\\1ex",
- "/(matr)ices$/i" => "\\1ix",
- "/(x|ch|ss|sh)es$/i" => "\\1",
- "/([m|l])ice$/i" => "\\1ouse",
- "/(quiz)zes$/i" => "\\1");
- $replaced = $string;
- foreach ($replacements as $key => $val) {
- if (in_array($string, $uncountable))
- break;
- $replaced = preg_replace($key, $val, $string);
- if ($replaced != $string)
- break;
- }
- if ($replaced == $string and !in_array($string, $uncountable))
- return substr($string, 0, -1);
- else
- return $replaced;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment