forked from Hemant-Mann/Musik
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringmethods.php
More file actions
233 lines (199 loc) · 7.54 KB
/
Copy pathstringmethods.php
File metadata and controls
233 lines (199 loc) · 7.54 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
namespace Framework {
/**
* Utility methods for working with the basic data types we find in PHP
*/
class StringMethods {
/**
*for the normalization of regular expression strings, so that the remaining methods can operate on them
* without first having to check or normalize them.
* @var type
*/
private static $_delimiter = "#";
private static $_singular = array(
"(matr)ices$" => "\\1ix",
"(vert|ind)ices$" => "\\1ex",
"^(ox)en" => "\\1",
"(alias)es$" => "\\1",
"([octop|vir])i$" => "\\1us",
"(cris|ax|test)es$" => "\\1is",
"(shoe)s$" => "\\1",
"(o)es$" => "\\1",
"(bus|campus)es$" => "\\1",
"([m|l])ice$" => "\\1ouse",
"(x|ch|ss|sh)es$" => "\\1",
"(m)ovies$" => "\\1\\2ovie",
"(s)eries$" => "\\1\\2eries",
"([^aeiouy]|qu)ies$" => "\\1y",
"([lr])ves$" => "\\1f",
"(tive)s$" => "\\1",
"(hive)s$" => "\\1",
"([^f])ves$" => "\\1fe",
"(^analy)ses$" => "\\1sis",
"((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$" => "\\1\\2sis",
"([ti])a$" => "\\1um",
"(p)eople$" => "\\1\\2erson",
"(m)en$" => "\\1an",
"(s)tatuses$" => "\\1\\2tatus",
"(c)hildren$" => "\\1\\2hild",
"(n)ews$" => "\\1\\2ews",
"([^u])s$" => "\\1"
);
private static $_plural = array(
"^(ox)$" => "\\1\\2en",
"([m|l])ouse$" => "\\1ice",
"(matr|vert|ind)ix|ex$" => "\\1ices",
"(x|ch|ss|sh)$" => "\\1es",
"([^aeiouy]|qu)y$" => "\\1ies",
"(hive)$" => "\\1s",
"(?:([^f])fe|([lr])f)$" => "\\1\\2ves",
"sis$" => "ses",
"([ti])um$" => "\\1a",
"(p)erson$" => "\\1eople",
"(m)an$" => "\\1en",
"(c)hild$" => "\\1hildren",
"(buffal|tomat)o$" => "\\1\\2oes",
"(bu|campu)s$" => "\\1\\2ses",
"(alias|status|virus)" => "\\1es",
"(octop)us$" => "\\1i",
"(ax|cris|test)is$" => "\\1es",
"s$" => "s",
"$" => "s"
);
private function __construct() {
// do nothing
}
private function __clone() {
// do nothing
}
/**
* For the normalization of regular expression strings, so that the remaining methods can operate on them
* without first having to check or normalize them.
* @param type $pattern
* @return type
*/
private static function _normalize($pattern) {
return self::$_delimiter . trim($pattern, self::$_delimiter) . self::$_delimiter;
}
public static function getDelimiter() {
return self::$_delimiter;
}
public static function setDelimiter($delimiter) {
self::$_delimiter = $delimiter;
}
/**
* Perform similarly to the preg_match_all() and preg_split() functions, but require less formal structure to the regular expressions,
* and return a more predictable set of results
*
* @param type $string
* @param type $pattern
* @return type return the first captured substring, the entire substring match, or null
*/
public static function match($string, $pattern) {
preg_match_all(self::_normalize($pattern), $string, $matches, PREG_PATTERN_ORDER);
if (!empty($matches[1])) {
return $matches[1];
}
if (!empty($matches[0])) {
return $matches[0];
}
return null;
}
/**
* Perform similarly to the preg_split() functions, but require less formal structure to the regular expressions,
* and return a more predictable set of results
*
* @param type $string
* @param type $pattern
* @param type $limit
* @return type return the results of a call to the preg_split() function, after setting some flags and normalizing the regular expression.
*/
public static function split($string, $pattern, $limit = null) {
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;
return preg_split(self::_normalize($pattern), $string, $limit, $flags);
}
public static function sanitize($string, $mask) {
if (is_array($mask)) {
$parts = $mask;
} else if (is_string($mask)) {
$parts = str_split($mask);
} else {
return $string;
}
foreach ($parts as $part) {
$normalized = self::_normalize("\\{$part}");
$string = preg_replace(
"{$normalized}m", "\\{$part}", $string
);
}
return $string;
}
public static function unique($string) {
$unique = "";
$parts = str_split($string);
foreach ($parts as $part) {
if (!strstr($unique, $part)) {
$unique .= $part;
}
}
return $unique;
}
public static function indexOf($string, $substring, $offset = null) {
$position = strpos($string, $substring, $offset);
if (!is_int($position)) {
return -1;
}
return $position;
}
public static function lastIndexOf($string, $substring, $offset = null) {
$position = strrpos($string, $substring, $offset);
if (!is_int($position)) {
return -1;
}
return $position;
}
public static function singular($string) {
$result = $string;
foreach (self::$_singular as $rule => $replacement) {
$rule = self::_normalize($rule);
if (preg_match($rule, $string)) {
$result = preg_replace($rule, $replacement, $string);
break;
}
}
return $result;
}
public static function plural($string) {
$result = $string;
foreach (self::$_plural as $rule => $replacement) {
$rule = self::_normalize($rule);
if (preg_match($rule, $string)) {
$result = preg_replace($rule, $replacement, $string);
break;
}
}
return $result;
}
public static function datetime_to_text($datetime = "") {
if ($datetime == '0000-00-00 00:00:00') {
return "Not Specified";
} else {
$unixdatetme = strtotime($datetime);
return strftime("%B %d %Y at %I:%M %p", $unixdatetme);
}
}
public static function only_date($datetime = "") {
if ($datetime == '0000-00-00 00:00:00') {
return 'Not Specified';
} else {
$unixdatetme = strtotime($datetime);
return strftime("%B %d, %Y", $unixdatetme);
}
}
public static function url($url) {
$pattern = array('?', '.', ':', '\'', '/', '(', ')', ',', '&');
$replace = array('', '', '', '', '', '', '', '', '');
return urlencode(str_replace($pattern, $replace, $url));
}
}
}