Created
February 11, 2015 17:18
-
-
Save freekrai/9cff69680213c7452049 to your computer and use it in GitHub Desktop.
Pluralize or depularize
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 | |
/** | |
* Inflector for pluralize and singularize English nouns. | |
* | |
* This Inflector is a port of Ruby on Rails Inflector. | |
* | |
* It can be really helpful for developers that want to | |
* create frameworks based on naming conventions rather than | |
* configurations. | |
* | |
*/ | |
class Inflector{ | |
function pluralize($word){ | |
$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)ies$/i' => '1y', | |
'/([^aeiouy]|qu)y$/i' => '1ies', | |
'/(hive)$/i' => '1s', | |
'/(?:([^f])fe|([lr])f)$/i' => '12ves', | |
'/sis$/i' => 'ses', | |
'/([ti])um$/i' => '1a', | |
'/(buffal|tomat)o$/i' => '1oes', | |
'/(bu)s$/i' => '1ses', | |
'/(alias|status)/i'=> '1es', | |
'/(octop|vir)us$/i'=> '1i', | |
'/(ax|test)is$/i'=> '1es', | |
'/s$/i'=> 's', | |
'/$/'=> 's' | |
); | |
$uncountable = array('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep'); | |
$irregular = array( | |
'person' => 'people', | |
'man' => 'men', | |
'child' => 'children', | |
'sex' => 'sexes', | |
'move' => 'moves' | |
); | |
$lowercased_word = strtolower($word); | |
foreach ($uncountable as $_uncountable){ | |
if(substr($lowercased_word,(-1*strlen($_uncountable))) == $_uncountable){ | |
return $word; | |
} | |
} | |
foreach ($irregular as $_plural=> $_singular){ | |
if (preg_match('/('.$_plural.')$/i', $word, $arr)) { | |
return preg_replace('/('.$_plural.')$/i', substr($arr[0],0,1).substr($_singular,1), $word); | |
} | |
} | |
foreach ($plural as $rule => $replacement) { | |
if (preg_match($rule, $word)) { | |
return preg_replace($rule, $replacement, $word); | |
} | |
} | |
return false; | |
} | |
function singularize($word){ | |
$singular = array ( | |
'/(quiz)zes$/i' => '\1', | |
'/(matr)ices$/i' => '\1ix', | |
'/(vert|ind)ices$/i' => '\1ex', | |
'/^(ox)en/i' => '\1', | |
'/(alias|status)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', | |
'/([^f])ves$/i' => '\1fe', | |
'/(^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', | |
'/s$/i' => '', | |
); | |
$uncountable = array('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep'); | |
$irregular = array( | |
'person' => 'people', | |
'man' => 'men', | |
'child' => 'children', | |
'sex' => 'sexes', | |
'move' => 'moves' | |
); | |
$lowercased_word = strtolower($word); | |
foreach ($uncountable as $_uncountable){ | |
if(substr($lowercased_word,(-1*strlen($_uncountable))) == $_uncountable){ | |
return $word; | |
} | |
} | |
foreach ($irregular as $_plural=> $_singular){ | |
if (preg_match('/('.$_singular.')$/i', $word, $arr)) { | |
return preg_replace('/('.$_singular.')$/i', substr($arr[0],0,1).substr($_plural,1), $word); | |
} | |
} | |
foreach ($singular as $rule => $replacement) { | |
if (preg_match($rule, $word)) { | |
return preg_replace($rule, $replacement, $word); | |
} | |
} | |
return $word; | |
} | |
function titleize($word, $uppercase = ''){ | |
$uppercase = $uppercase == 'first' ? 'ucfirst' : 'ucwords'; | |
return $uppercase(Inflector::humanize(Inflector::underscore($word))); | |
} | |
function camelize($word){ | |
return str_replace(' ','',ucwords(preg_replace('/[^A-Z^a-z^0-9]+/',' ',$word))); | |
} | |
function underscore($word){ | |
return strtolower(preg_replace('/[^A-Z^a-z^0-9]+/','_', | |
preg_replace('/([a-zd])([A-Z])/','1_2', | |
preg_replace('/([A-Z]+)([A-Z][a-z])/','1_2',$word)))); | |
} | |
function humanize($word, $uppercase = ''){ | |
$uppercase = $uppercase == 'all' ? 'ucwords' : 'ucfirst'; | |
return $uppercase(str_replace('_',' ',preg_replace('/_id$/', '',$word))); | |
} | |
function variablize($word){ | |
$word = Inflector::camelize($word); | |
return strtolower($word[0]).substr($word,1); | |
} | |
function tableize($class_name){ | |
return Inflector::pluralize(Inflector::underscore($class_name)); | |
} | |
function classify($table_name){ | |
return Inflector::camelize(Inflector::singularize($table_name)); | |
} | |
function ordinalize($number){ | |
if (in_array(($number % 100),range(11,13))){ | |
return $number.'th'; | |
}else{ | |
switch (($number % 10)) { | |
case 1: | |
return $number.'st'; break; | |
case 2: | |
return $number.'nd'; break; | |
case 3: | |
return $number.'rd'; break; | |
default: | |
return $number.'th'; break; | |
} | |
} | |
} | |
} | |
// Usage Examples | |
/* Singular to plural / Plural to singular */ | |
echo Inflector::pluralize('search'); // outputs searches | |
echo Inflector::singularize('cases'); // outputs case | |
echo Inflector::pluralize('query'); // outputs queries | |
echo Inflector::singularize('queries'); // outputs query | |
echo Inflector::pluralize('ability'); // outputs abilities | |
echo Inflector::singularize('abilities'); // outputs ability | |
echo Inflector::pluralize('analysis'); // outputs analyses | |
echo Inflector::singularize('analyses'); // outputs analysis | |
echo Inflector::pluralize('information'); // outputs information | |
echo Inflector::singularize('information'); // outputs information | |
echo Inflector::pluralize('mouse'); // outputs mice | |
echo Inflector::singularize('mice'); // outputs mouse | |
/* CamelCase to underscore / underscore to CamelCase */ | |
echo Inflector::underscore('SpecialGuest'); // outputs special_guest | |
echo Inflector::camelize('special_guest'); // outputs SpecialGuest | |
echo Inflector::underscore('FreeBSD'); // outputs free_bsd | |
echo Inflector::camelize('free_bsd'); // outputs FreeBsd | |
echo Inflector::underscore('HTML'); // outputs html | |
echo Inflector::camelize('html'); // outputs Html | |
/* Underscore to "human-text" / "Human-text" to Underscore */ | |
echo Inflector::humanize('employee_salary'); // outputs Employee salary | |
echo Inflector::underscore('Employee salary'); // outputs employee_salary | |
/* Examples of titleize() */ | |
echo Inflector::titleize('ActiveRecord'); // outputs Active Record | |
echo Inflector::titleize('action web service'); // outputs Action Web Service | |
/* Examples of ordinalize() */ | |
echo Inflector::ordinalize(1); // outputs 1st | |
echo Inflector::ordinalize(2); // outputs 2nd | |
echo Inflector::ordinalize(3); // outputs 3rd | |
echo Inflector::ordinalize(4); // outputs 4th | |
echo Inflector::ordinalize(5); // outputs 5th | |
echo Inflector::ordinalize(20); // outputs 20th | |
echo Inflector::ordinalize(21); // outputs 21st |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment