VersionPress\Utils\StringUtils::pluralize PHP Method

pluralize() public static method

Note: It's very, very simplified! From: https://gist.github.com/tbrianjones/ba0460cc1d55f357e00b
public static pluralize ( string $string ) : string
$string string
return string
    public static function pluralize($string)
    {
        $plural = ['/(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"];
        $irregular = ['move' => 'moves', 'foot' => 'feet', 'goose' => 'geese', 'sex' => 'sexes', 'child' => 'children', 'man' => 'men', 'tooth' => 'teeth', 'person' => 'people', 'valve' => 'valves'];
        $uncountable = ['sheep', 'fish', 'deer', 'series', 'species', 'money', 'rice', 'information', 'equipment', 'meta'];
        // save some time in the case that singular and plural are the same
        if (in_array(strtolower($string), $uncountable)) {
            return $string;
        }
        // check for irregular singular forms
        foreach ($irregular as $pattern => $result) {
            $pattern = '/' . $pattern . '$/i';
            if (preg_match($pattern, $string)) {
                return preg_replace($pattern, $result, $string);
            }
        }
        // check for matches using regular expressions
        foreach ($plural as $pattern => $result) {
            if (preg_match($pattern, $string)) {
                return preg_replace($pattern, $result, $string);
            }
        }
        return $string;
    }

Usage Example

Esempio n. 1
0
 public function getChangeDescription()
 {
     if ($this->count === 1) {
         return $this->changeInfos[0]->getChangeDescription();
     }
     return sprintf("%s %d %s", Strings::capitalize(StringUtils::verbToPastTense($this->getAction())), $this->count, StringUtils::pluralize($this->getEntityName()));
 }
All Usage Examples Of VersionPress\Utils\StringUtils::pluralize