-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpluralize.php
More file actions
90 lines (80 loc) · 2.21 KB
/
Copy pathpluralize.php
File metadata and controls
90 lines (80 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* PHP pularisation helpers
*
* Created by dave@freshsauce.co.uk on 2012-09-17.
*/
class Pluralize {
static private $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")
);
static private $irregular = array(
array('move', 'moves'),
array('sex', 'sexes'),
array('child', 'children'),
array('man', 'men'),
array('woman', 'women'),
array('person', 'people'),
array('datum', 'data')
);
static private $uncountable = array(
'sheep',
'fish',
'series',
'species',
'money',
'rice',
'information',
'equipment'
);
/**
* no need for a constructor so private
*
*/
private function __construct() {
# code...
}
/**
* given a singular word and a count, returns the pural version of the word if appropriate
*
* @param string $string
* @param integer $count
* @return string original singlular word or pluralised version if appropriate
*/
static public function pluralize($string, $count = 2) {
if ($count == 1) return $string; // If only one no need to puralise
// save some time in the case that singular and plural are the same
if (in_array(strtolower($string), self::$uncountable))
return $string;
// check for irregular singular forms
foreach (self::$irregular as $noun)
{
if (strtolower($string) == $noun[0])
return $noun[1];
}
// check for matches using regular expressions
foreach (self::$plural as $pattern)
{
if (preg_match($pattern[0], $string))
return preg_replace($pattern[0], $pattern[1], $string);
}
return $string;
}
}