Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update Pluralize.php

  • Loading branch information...
commit 2dd63d9322959a3ca021889b30b87914245294c3 1 parent cf08f80
@maniator authored
Showing with 114 additions and 60 deletions.
  1. +114 −60 lib/Pluralize.php
View
174 lib/Pluralize.php
@@ -1,86 +1,140 @@
<?php
namespace SmallFry\lib;
/**
- * Description of Pluralize
+ * Thanks to http://www.eval.ca/articles/php-pluralize (MIT license)
+ * http://dev.rubyonrails.org/browser/trunk/activesupport/lib/active_support/inflections.rb (MIT license)
+ * http://www.fortunecity.com/bally/durrus/153/gramch13.html
+ * http://www2.gsu.edu/~wwwesl/egw/crump.htm
+ *
+ * @source http://kuwamoto.org/2007/12/17/improved-pluralizing-in-php-actionscript-and-ror/
*
- * Not used yet...
- *
- * @author nlubin
*/
-class Pluralize
+class Pluralize
{
-
- public function __construct(){}
-
- public static function conditionallyPluralize( $string, $count )
- {
- if ( intval( $count ) !== 0 )
- return MyClass::pluralize( $string );
+ static $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"
+ );
- return $string;
- }
+ static $singular = array(
+ '/(quiz)zes$/i' => "$1",
+ '/(matr)ices$/i' => "$1ix",
+ '/(vert|ind)ices$/i' => "$1ex",
+ '/^(ox)en$/i' => "$1",
+ '/(alias)es$/i' => "$1",
+ '/(octop|vir)i$/i' => "$1us",
+ '/(cris|ax|test)es$/i' => "$1is",
+ '/(shoe)s$/i' => "$1",
+ '/(o)es$/i' => "$1",
+ '/(bus)es$/i' => "$1",
+ '/([m|l])ice$/i' => "$1ouse",
+ '/(x|ch|ss|sh)es$/i' => "$1",
+ '/(m)ovies$/i' => "$1ovie",
+ '/(s)eries$/i' => "$1eries",
+ '/([^aeiouy]|qu)ies$/i' => "$1y",
+ '/([lr])ves$/i' => "$1f",
+ '/(tive)s$/i' => "$1",
+ '/(hive)s$/i' => "$1",
+ '/(li|wi|kni)ves$/i' => "$1fe",
+ '/(shea|loa|lea|thie)ves$/i'=> "$1f",
+ '/(^analy)ses$/i' => "$1sis",
+ '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => "$1$2sis",
+ '/([ti])a$/i' => "$1um",
+ '/(n)ews$/i' => "$1ews",
+ '/(h|bl)ouses$/i' => "$1ouse",
+ '/(corpse)s$/i' => "$1",
+ '/(us)es$/i' => "$1",
+ '/s$/i' => ""
+ );
+
+ static $irregular = array(
+ 'move' => 'moves',
+ 'foot' => 'feet',
+ 'goose' => 'geese',
+ 'sex' => 'sexes',
+ 'child' => 'children',
+ 'man' => 'men',
+ 'tooth' => 'teeth',
+ 'person' => 'people'
+ );
+
+ static $uncountable = array(
+ 'sheep',
+ 'fish',
+ 'deer',
+ 'series',
+ 'species',
+ 'money',
+ 'rice',
+ 'information',
+ 'equipment'
+ );
public static function pluralize( $string )
{
+ // save some time in the case that singular and plural are the same
+ if ( in_array( strtolower( $string ), self::$uncountable ) )
+ return $string;
- $plural = array(
- array( '/(quiz)$/i', "$1zes" ),
- array( '/^(ox)$/i', "$1en" ),
- array( '/([m|l])ouse$/i', "$1ice" ),
- array( '/(matr|vert|ind)ix|ex$/i', "$1ices" ),
- array( '/(x|ch|ss|sh)$/i', "$1es" ),
- array( '/([^aeiouy]|qu)y$/i', "$1ies" ),
- array( '/([^aeiouy]|qu)ies$/i', "$1y" ),
- array( '/(hive)$/i', "$1s" ),
- array( '/(?:([^f])fe|([lr])f)$/i', "$1$2ves" ),
- array( '/sis$/i', "ses" ),
- array( '/([ti])um$/i', "$1a" ),
- array( '/(buffal|tomat)o$/i', "$1oes" ),
- array( '/(bu)s$/i', "$1ses" ),
- array( '/(alias|status)$/i', "$1es" ),
- array( '/(octop|vir)us$/i', "$1i" ),
- array( '/(ax|test)is$/i', "$1es" ),
- array( '/s$/i', "s" ),
- array( '/$/', "s" )
- );
- $irregular = array(
- array( 'move', 'moves' ),
- array( 'sex', 'sexes' ),
- array( 'child', 'children' ),
- array( 'man', 'men' ),
- array( 'person', 'people' )
- );
+ // check for irregular singular forms
+ foreach ( self::$irregular as $pattern => $result )
+ {
+ $pattern = '/' . $pattern . '$/i';
- $uncountable = array(
- 'sheep',
- 'fish',
- 'series',
- 'species',
- 'money',
- 'rice',
- 'information',
- 'equipment'
- );
+ if ( preg_match( $pattern, $string ) )
+ return preg_replace( $pattern, $result, $string);
+ }
+ // check for matches using regular expressions
+ foreach ( self::$plural as $pattern => $result )
+ {
+ if ( preg_match( $pattern, $string ) )
+ return preg_replace( $pattern, $result, $string );
+ }
+
+ return $string;
+ }
+
+ public static function singularize( $string )
+ {
// save some time in the case that singular and plural are the same
- if ( in_array( strtolower( $string ), $uncountable ) )
+ if ( in_array( strtolower( $string ), self::$uncountable ) )
return $string;
- // check for irregular singular forms
- foreach ( $irregular as $noun )
+ // check for irregular plural forms
+ foreach ( self::$irregular as $result => $pattern )
{
- if ( strtolower( $string ) == $noun[0] )
- return $noun[1];
+ $pattern = '/' . $pattern . '$/i';
+
+ if ( preg_match( $pattern, $string ) )
+ return preg_replace( $pattern, $result, $string);
}
// check for matches using regular expressions
- foreach ( $plural as $pattern )
+ foreach ( self::$singular as $pattern => $result )
{
- if ( preg_match( $pattern[0], $string ) )
- return preg_replace( $pattern[0], $pattern[1], $string );
+ if ( preg_match( $pattern, $string ) )
+ return preg_replace( $pattern, $result, $string );
}
-
+
return $string;
}
Please sign in to comment.
Something went wrong with that request. Please try again.