|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Rico\Slib; |
|
6
|
|
|
|
|
7
|
|
|
abstract class StringUtils |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Converts an alphabetic $string into an identifier (an integer). |
|
11
|
|
|
* |
|
12
|
|
|
* @param string $string |
|
13
|
|
|
* @param string $secret to decode the alphabetic string |
|
14
|
|
|
* |
|
15
|
|
|
* @return int |
|
16
|
|
|
*/ |
|
17
|
|
|
public static function alphaToId(string $string, string $secret = ''): int |
|
18
|
|
|
{ |
|
19
|
|
|
$out = 0; |
|
20
|
|
|
$index = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
21
|
|
|
$base = mb_strlen($index); |
|
22
|
|
|
$stringLength = mb_strlen($string) - 1; |
|
23
|
|
|
|
|
24
|
|
|
if ($secret) { |
|
25
|
|
|
$splitIndex = str_split($index); |
|
26
|
|
|
|
|
27
|
|
|
$array = array_slice(str_split(hash('sha512', $secret)), 0, $base); |
|
28
|
|
|
array_multisort($array, SORT_DESC, $splitIndex); |
|
29
|
|
|
$index = implode($splitIndex); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
for ($t = $stringLength; $t >= 0; --$t) { |
|
33
|
|
|
$bcp = $base ** ($stringLength - $t); |
|
34
|
|
|
$out += mb_strpos($index, mb_substr($string, $t, 1)) * $bcp; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $out; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Transforms an $uglyString (with incorrect ponctuation) into beautiful string (with correct ponctuation). |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $uglyString |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function beautifulise(string $uglyString): string |
|
48
|
|
|
{ |
|
49
|
|
|
$uglyString = self::normalizeWhitespace($uglyString); |
|
50
|
|
|
// Be careful, there are non secable spaces here |
|
51
|
|
|
$uglyString = str_replace(['\'\'', ' ;', ' ?', ' !', ' :', ' »', '« ', '\'', '...'], ['"', ' ;', ' ?', ' !', ' :', ' »', '« ', '’', '…'], $uglyString); |
|
52
|
|
|
$uglyString = preg_replace('#(\d)\s?([$€£¥])#u', '$1 $2', $uglyString); |
|
53
|
|
|
$uglyString = preg_replace_callback('#\d{4,}#u', function ($matches) { |
|
54
|
|
|
return number_format((float) $matches[0], 0, ',', ' '); |
|
55
|
|
|
}, $uglyString); |
|
56
|
|
|
|
|
57
|
|
|
// Count quotes |
|
58
|
|
|
$QuotesCount = mb_strlen($uglyString) - mb_strlen(str_replace('"', '', $uglyString)); |
|
59
|
|
|
|
|
60
|
|
|
// Repeat two times is important for quotes inside quotes |
|
61
|
|
|
if ($QuotesCount % 2 == 0) { |
|
62
|
|
|
$uglyString = preg_replace('#([\s\r\n\p{P}]|^|)(\")([^\"]*)(\")([\s\p{P}]|$)#u', '$1“$3”$5', $uglyString); |
|
63
|
|
|
$uglyString = preg_replace('#([\s\r\n\p{P}]|^|)(\")([^\"]*)(\")([\s\p{P}]|$)#u', '$1“$3”$5', $uglyString); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $uglyString; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Gets a human readable string of a size in $bytes. |
|
71
|
|
|
* |
|
72
|
|
|
* @param int $bytes |
|
73
|
|
|
* |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function humanFilesize(int $bytes): string |
|
77
|
|
|
{ |
|
78
|
|
|
$size = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; |
|
79
|
|
|
$factor = floor((mb_strlen((string) $bytes) - 1) / 3); |
|
80
|
|
|
|
|
81
|
|
|
return MathUtils::smartRound($bytes / pow(1000, $factor)).@$size[$factor]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Converts a $identifier into an alphanumeric string. |
|
86
|
|
|
* |
|
87
|
|
|
* @param int $identifier |
|
88
|
|
|
* @param string $secret to encode the integer |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
public static function idToAlpha(int $identifier, string $secret = ''): string |
|
93
|
|
|
{ |
|
94
|
|
|
$out = ''; |
|
95
|
|
|
$index = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
96
|
|
|
$base = mb_strlen($index); |
|
97
|
|
|
|
|
98
|
|
|
if ($secret) { |
|
99
|
|
|
$splitIndex = str_split($index); |
|
100
|
|
|
|
|
101
|
|
|
$array = array_slice(str_split(hash('sha512', $secret)), 0, $base); |
|
102
|
|
|
array_multisort($array, SORT_DESC, $splitIndex); |
|
103
|
|
|
$index = implode($splitIndex); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
for ($t = ($identifier != 0 ? floor(log($identifier, $base)) : 0); $t >= 0; --$t) { |
|
107
|
|
|
$bcp = $base ** $t; |
|
108
|
|
|
$a = floor($identifier / $bcp) % $base; |
|
109
|
|
|
$out .= mb_substr($index, $a, 1); |
|
110
|
|
|
$identifier = $identifier - ($a * $bcp); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $out; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Removes whitespaces, line breaks and comment out of a $string. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $string |
|
120
|
|
|
* |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
|
|
public static function minify(string $string): string |
|
124
|
|
|
{ |
|
125
|
|
|
$string = preg_replace('#\/\*.*\*\/#s', '', $string); |
|
126
|
|
|
$string = self::removeLine($string); |
|
127
|
|
|
$string = self::normalizeWhitespace($string); |
|
128
|
|
|
$string = preg_replace('# ?([\;\:\{\}\,]) ?#', '$1', $string); |
|
129
|
|
|
|
|
130
|
|
|
return $string; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Cleans a $string by removing multi-spaces, line breaks, indents and HTML tags. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $string |
|
137
|
|
|
* |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
|
|
public static function normalize(string $string): string |
|
141
|
|
|
{ |
|
142
|
|
|
$string = str_replace(['<br/>', '<br />', '</br>', '<br>', '<br >', '< br >'], ' ', $string); |
|
143
|
|
|
$string = html_entity_decode($string, ENT_HTML5, 'UTF-8'); |
|
144
|
|
|
$string = strip_tags($string); |
|
145
|
|
|
$string = self::removeLine($string); |
|
146
|
|
|
$string = self::normalizeWhitespace($string); |
|
147
|
|
|
|
|
148
|
|
|
return $string; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Replaces all sort of spaces (tab, nil, non-breaking…) in a $string by a simple space. |
|
153
|
|
|
* |
|
154
|
|
|
* @param string $string |
|
155
|
|
|
* |
|
156
|
|
|
* @return string |
|
157
|
|
|
*/ |
|
158
|
|
|
public static function normalizeWhitespace(string $string): string |
|
159
|
|
|
{ |
|
160
|
|
|
/* |
|
161
|
|
|
* \0 : NIL char |
|
162
|
|
|
* \xC2 : non-breaking space |
|
163
|
|
|
* \xA0 : non-breaking space |
|
164
|
|
|
* \x0B : vertical tab |
|
165
|
|
|
* \t : tab |
|
166
|
|
|
*/ |
|
167
|
|
|
return trim(preg_replace('/[\0\xC2\xA0\x0B\t\ \ \ \]+/u', ' ', $string)); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Generates a random string of $length $allowedChars. |
|
172
|
|
|
* |
|
173
|
|
|
* @param int $length |
|
174
|
|
|
* @param string $allowedChars |
|
175
|
|
|
* |
|
176
|
|
|
* @return string |
|
177
|
|
|
*/ |
|
178
|
|
|
public static function randString(int $length = 10, string $allowedChars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'): string |
|
179
|
|
|
{ |
|
180
|
|
|
if ($length <= 0) { |
|
181
|
|
|
return ''; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
$randString = ''; |
|
185
|
|
|
$allowedCharsLength = mb_strlen($allowedChars, 'UTF-8'); |
|
186
|
|
|
for ($i = 0; $i < $length; ++$i) { |
|
187
|
|
|
$randString .= mb_substr($allowedChars, mt_rand(0, ($allowedCharsLength - 1)), 1, 'UTF-8'); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return $randString; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Removes brackets and its content from a $string. |
|
195
|
|
|
* |
|
196
|
|
|
* @param string $string |
|
197
|
|
|
* |
|
198
|
|
|
* @return string |
|
199
|
|
|
*/ |
|
200
|
|
|
public static function removeBracketContent(string $string): string |
|
201
|
|
|
{ |
|
202
|
|
|
return trim(preg_replace("#\s*\[[^)]+\]\s*#", '', $string)); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Removes all sort of line breaks inside a $string. |
|
207
|
|
|
* |
|
208
|
|
|
* @param string $string |
|
209
|
|
|
* |
|
210
|
|
|
* @return string |
|
211
|
|
|
*/ |
|
212
|
|
|
public static function removeLine(string $string): string |
|
213
|
|
|
{ |
|
214
|
|
|
return preg_replace('/[\r\n]+/', '', $string); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Removes all sort of spaces from a $string. |
|
219
|
|
|
* |
|
220
|
|
|
* @param string $string |
|
221
|
|
|
* |
|
222
|
|
|
* @return string |
|
223
|
|
|
*/ |
|
224
|
|
|
public static function removeWhitespace(string $string): string |
|
225
|
|
|
{ |
|
226
|
|
|
/* |
|
227
|
|
|
* \0 : NIL char |
|
228
|
|
|
* \xC2 : non-breaking space |
|
229
|
|
|
* \xA0 : non-breaking space |
|
230
|
|
|
* \x0B : vertical tab |
|
231
|
|
|
* \t : tab |
|
232
|
|
|
*/ |
|
233
|
|
|
return preg_replace('/[\0\xC2\xA0\x0B\t\ \ \ \]+/u', '', $string); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Transforms a $string into a ascii-only string separated by -. |
|
238
|
|
|
* |
|
239
|
|
|
* @param string $string |
|
240
|
|
|
* @return string |
|
241
|
|
|
* @throws \Exception |
|
242
|
|
|
*/ |
|
243
|
|
|
public static function slugify(string $string): string |
|
244
|
|
|
{ |
|
245
|
|
|
setlocale(LC_CTYPE, 'UTF-8'); |
|
246
|
|
|
|
|
247
|
|
|
// replace non letter or digits by - |
|
248
|
|
|
$string = preg_replace('#[^\\pL\d]+#u', '-', $string); |
|
249
|
|
|
|
|
250
|
|
|
$transliterate_string = transliterator_transliterate("Any-Latin; Latin-ASCII; NFD; [:Nonspacing Mark:] Remove; NFC; Lower();", $string); |
|
251
|
|
|
|
|
252
|
|
|
if (false === $transliterate_string) { |
|
253
|
|
|
throw new \Exception('This function (' . __FUNCTION__ .') was not able to transliterate “'. $string .'”'); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
$string = trim($transliterate_string, '-'); |
|
257
|
|
|
|
|
258
|
|
|
return $string; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Replaces underscore in $string by spaces. |
|
263
|
|
|
* |
|
264
|
|
|
* @param string $string |
|
265
|
|
|
* |
|
266
|
|
|
* @return string |
|
267
|
|
|
*/ |
|
268
|
|
|
public static function underscoreToSpace(string $string): string |
|
269
|
|
|
{ |
|
270
|
|
|
$string = trim(str_replace('_', ' ', $string)); |
|
271
|
|
|
$string = self::normalizeWhitespace($string); |
|
272
|
|
|
|
|
273
|
|
|
return $string; |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|