Guest User

Untitled

a guest
Jan 5th, 2012
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. <?php
  2. /**
  3. * Function: pluralize
  4. * Returns a pluralized string. This is a port of Rails' pluralizer.
  5. *
  6. * Parameters:
  7. * $string - The string to pluralize.
  8. */
  9. function pluralize($string) {
  10. global $pluralizations;
  11. if (in_array($string, array_keys($pluralizations)))
  12. return $pluralizations[$string];
  13. else {
  14. $uncountable = array("moose", "sheep", "fish", "series", "species", "rice", "money", "information", "equipment", "piss");
  15. $replacements = array("/person/i" => "people",
  16. "/man/i" => "men",
  17. "/child/i" => "children",
  18. "/cow/i" => "kine",
  19. "/goose/i" => "geese",
  20. "/(ax|test)is$/i" => "\\1es",
  21. "/(octop|vir)us$/i" => "\\1ii",
  22. "/(alias|status)$/i" => "\\1es",
  23. "/(bu)s$/i" => "\\1ses",
  24. "/(buffal|tomat)o$/i" => "\\1oes",
  25. "/([ti])um$/i" => "\\1a",
  26. "/sis$/i" => "ses",
  27. "/(hive)$/i" => "\\1s",
  28. "/([^aeiouy]|qu)y$/i" => "\\1ies",
  29. "/^(ox)$/i" => "\\1en",
  30. "/(matr|vert|ind)(?:ix|ex)$/i" => "\\1ices",
  31. "/(x|ch|ss|sh)$/i" => "\\1es",
  32. "/([m|l])ouse$/i" => "\\1ice",
  33. "/(quiz)$/i" => "\\1zes");
  34.  
  35. $replaced = $string;
  36. foreach ($replacements as $key => $val) {
  37. if (in_array($string, $uncountable))
  38. break;
  39.  
  40. $replaced = preg_replace($key, $val, $string);
  41.  
  42. if ($replaced != $string)
  43. break;
  44. }
  45.  
  46. if ($replaced == $string and !in_array($string, $uncountable))
  47. return $string."s";
  48. else
  49. return $replaced;
  50. }
  51. }
  52.  
  53. /**
  54. * Function: depluralize
  55. * Look in $pluralizations for a depluralization of $string.
  56. *
  57. * Parameters:
  58. * $string - The string to depluralize.
  59. */
  60. function depluralize($string) {
  61. global $pluralizations;
  62.  
  63. $copy = $pluralizations;
  64. unset($copy["feathers"]);
  65. $reversed = array_flip($copy);
  66.  
  67. if (isset($reversed[$string]))
  68. return $reversed[$string];
  69. else {
  70. $uncountable = array("moose", "sheep", "fish", "series", "species", "rice", "money", "information", "equipment", "piss");
  71. $replacements = array("/people/i" => "person",
  72. "/^men/i" => "man",
  73. "/children/i" => "child",
  74. "/kine/i" => "cow",
  75. "/geese/i" => "goose",
  76. "/(ax|test)es$/i" => "\\1is",
  77. "/(octop|vir)ii$/i" => "\\1us",
  78. "/(alias|status)es$/i" => "\\1",
  79. "/(bu)ses$/i" => "\\1s",
  80. "/(buffal|tomat)oes$/i" => "\\1o",
  81. "/([ti])a$/i" => "\\1um",
  82. "/ses$/i" => "sis",
  83. "/(hive)s$/i" => "\\1",
  84. "/([^aeiouy]|qu)ies$/i" => "\\1y",
  85. "/^(ox)en$/i" => "\\1",
  86. "/(vert|ind)ices$/i" => "\\1ex",
  87. "/(matr)ices$/i" => "\\1ix",
  88. "/(x|ch|ss|sh)es$/i" => "\\1",
  89. "/([m|l])ice$/i" => "\\1ouse",
  90. "/(quiz)zes$/i" => "\\1");
  91.  
  92. $replaced = $string;
  93. foreach ($replacements as $key => $val) {
  94. if (in_array($string, $uncountable))
  95. break;
  96.  
  97. $replaced = preg_replace($key, $val, $string);
  98.  
  99. if ($replaced != $string)
  100. break;
  101. }
  102.  
  103. if ($replaced == $string and !in_array($string, $uncountable))
  104. return substr($string, 0, -1);
  105. else
  106. return $replaced;
  107. }
  108. }
  109. ?>
Advertisement
Add Comment
Please, Sign In to add comment