Created
January 19, 2012 03:19
-
-
Save MilkZoft/1637520 to your computer and use it in GitHub Desktop.
codejobs - Pluralize Words - PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function pluralize($word) { | |
| $uncountable = array("sheep", | |
| "fish", | |
| "deer", | |
| "series", | |
| "species", | |
| "money", | |
| "rice", | |
| "information", | |
| "equipment" | |
| ); | |
| $irregular = array("move" => "moves", | |
| "foot" => "feet", | |
| "goose" => "geese", | |
| "sex" => "sexes", | |
| "child" => "children", | |
| "man" => "men", | |
| "tooth" => "teeth", | |
| "person" => "people" | |
| ); | |
| $plural = array( "/(quiz)$/i" => "$1zes", | |
| "/^(ox)$/i" => "$1en", | |
| "/([m|l])ouse$/i" => "$1ice", | |
| "/(matr|vert|ind)ix|ex$/i" => "$1ices", | |
| "/(x|ch|ss|sh)$/i" => "$1es", | |
| "/([^aeiouy]|qu)y$/i" => "$1ies", | |
| "/(hive)$/i" => "$1s", | |
| "/(?:([^f])fe|([lr])f)$/i" => "$1$2ves", | |
| "/(shea|lea|loa|thie)f$/i" => "$1ves", | |
| "/sis$/i" => "ses", | |
| "/([ti])um$/i" => "$1a", | |
| "/(tomat|potat|ech|her|vet)o$/i" => "$1oes", | |
| "/(bu)s$/i" => "$1ses", | |
| "/(alias)$/i" => "$1es", | |
| "/(octop)us$/i" => "$1i", | |
| "/(ax|test)is$/i" => "$1es", | |
| "/(us)$/i" => "$1es", | |
| "/s$/i" => "s", | |
| "/$/" => "s" | |
| ); | |
| if(in_array(strtolower($word), $uncountable)) { | |
| return $word; | |
| } | |
| foreach($irregular as $pattern => $result) { | |
| $pattern = "/$pattern$/i"; | |
| if(preg_match($pattern, $word)) { | |
| return preg_replace($pattern, $result, $word); | |
| } | |
| } | |
| foreach($plural as $pattern => $result) { | |
| if(preg_match($pattern, $word)) { | |
| return preg_replace($pattern, $result, $word); | |
| } | |
| } | |
| return $word; | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment