Skip to content

Instantly share code, notes, and snippets.

@orlov0562
Created February 6, 2014 23:12
Show Gist options
  • Save orlov0562/8854449 to your computer and use it in GitHub Desktop.
Save orlov0562/8854449 to your computer and use it in GitHub Desktop.
// From PHPixie ORM [http://phpixie.com]
/**
* Gets plural form of a noun
*
* @param string $str Noun to get a plural form of
* @return string Plural form
*/
protected function plural($str)
{
$regexes = array(
'/^(.*?[sxz])$/i' => '\\1es',
'/^(.*?[^aeioudgkprt]h)$/i' => '\\1es',
'/^(.*?[^aeiou])y$/i' => '\\1ies',
);
foreach ($regexes as $key => $val)
{
$str = preg_replace($key, $val, $str, -1, $count);
if ($count)
{
return $str;
}
}
return $str.'s';
}
/**
* Gets singular form of a noun
*
* @param string $str Noun to get singular form of
* @return string Singular form of the noun
*/
protected function singular($str)
{
$regexes = array(
'/^(.*?us)$/i' => '\\1',
'/^(.*?[sxz])es$/i' => '\\1',
'/^(.*?[^aeioudgkprt]h)es$/i' => '\\1',
'/^(.*?[^aeiou])ies$/i' => '\\1y',
'/^(.*?)s$/' => '\\1',
);
foreach ($regexes as $key => $val)
{
$str = preg_replace($key, $val, $str, -1, $count);
if ($count)
{
return $str;
}
}
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment