|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AppUtils; |
|
6
|
|
|
|
|
7
|
|
|
use ForceUTF8\Encoding; |
|
8
|
|
|
|
|
9
|
|
|
class ConvertHelper_String |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Searches for needle in the specified string, and returns a list |
|
13
|
|
|
* of all occurrences, including the matched string. The matched |
|
14
|
|
|
* string is useful when doing a case insensitive search, as it |
|
15
|
|
|
* shows the exact matched case of needle. |
|
16
|
|
|
* |
|
17
|
|
|
* @param string $needle |
|
18
|
|
|
* @param string $haystack |
|
19
|
|
|
* @param bool $caseInsensitive |
|
20
|
|
|
* @return ConvertHelper_StringMatch[] |
|
21
|
|
|
*/ |
|
22
|
|
|
public static function findString(string $needle, string $haystack, bool $caseInsensitive=false): array |
|
23
|
|
|
{ |
|
24
|
|
|
if($needle === '') { |
|
25
|
|
|
return array(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$function = 'mb_strpos'; |
|
29
|
|
|
if($caseInsensitive) { |
|
30
|
|
|
$function = 'mb_stripos'; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$pos = 0; |
|
34
|
|
|
$positions = array(); |
|
35
|
|
|
$length = mb_strlen($needle); |
|
36
|
|
|
|
|
37
|
|
|
while( ($pos = $function($haystack, $needle, $pos)) !== false) |
|
38
|
|
|
{ |
|
39
|
|
|
$match = mb_substr($haystack, $pos, $length); |
|
40
|
|
|
$positions[] = new ConvertHelper_StringMatch($pos, $match); |
|
41
|
|
|
$pos += $length; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $positions; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Splits a string into an array of all characters it is composed of. |
|
49
|
|
|
* Unicode character safe. |
|
50
|
|
|
* |
|
51
|
|
|
* NOTE: Spaces and newlines (both \r and \n) are also considered single |
|
52
|
|
|
* characters. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $string |
|
55
|
|
|
* @return string[] |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function toArray(string $string) : array |
|
58
|
|
|
{ |
|
59
|
|
|
$result = preg_split('//u', $string, 0, PREG_SPLIT_NO_EMPTY); |
|
60
|
|
|
if($result !== false) { |
|
61
|
|
|
return $result; |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return array(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Calculates the byte length of a string, taking into |
|
69
|
|
|
* account any unicode characters. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $string |
|
72
|
|
|
* @return int |
|
73
|
|
|
* @see https://stackoverflow.com/a/9718273/2298192 |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function toBytes(string $string) : int |
|
76
|
|
|
{ |
|
77
|
|
|
return mb_strlen($string, '8bit'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Converts a string into an MD5 hash. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $string |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function toHash(string $string): string |
|
87
|
|
|
{ |
|
88
|
|
|
return md5($string); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Creates a short, 8-character long hash for the specified string. |
|
93
|
|
|
* |
|
94
|
|
|
* WARNING: Not cryptographically safe. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $string |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
public static function toShortHash(string $string) : string |
|
100
|
|
|
{ |
|
101
|
|
|
return hash('crc32', $string, false); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Converts a string to valid utf8, regardless |
|
106
|
|
|
* of the string's encoding(s). |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $string |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
public static function toUtf8(string $string) : string |
|
112
|
|
|
{ |
|
113
|
|
|
if(!self::isASCII($string)) { |
|
114
|
|
|
return Encoding::toUTF8($string); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $string; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Checks whether the specified string is an ASCII |
|
122
|
|
|
* string, without any special or UTF8 characters. |
|
123
|
|
|
* Note: empty strings and NULL are considered ASCII. |
|
124
|
|
|
* Any variable types other than strings are not. |
|
125
|
|
|
* |
|
126
|
|
|
* @param mixed $string |
|
127
|
|
|
* @return boolean |
|
128
|
|
|
*/ |
|
129
|
|
|
public static function isASCII(string $string) : bool |
|
130
|
|
|
{ |
|
131
|
|
|
if($string === '' || $string === NULL) { |
|
132
|
|
|
return true; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
if(!is_string($string)) { |
|
|
|
|
|
|
136
|
|
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return !preg_match('/[^\x00-\x7F]/', $string); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Checks whether the specified string contains HTML code. |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $string |
|
146
|
|
|
* @return boolean |
|
147
|
|
|
*/ |
|
148
|
|
|
public static function isHTML(string $string) : bool |
|
149
|
|
|
{ |
|
150
|
|
|
if(preg_match('%<[a-z/][\s\S]*>%siU', $string)) { |
|
151
|
|
|
return true; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$decoded = html_entity_decode($string); |
|
155
|
|
|
if($decoded !== $string) { |
|
156
|
|
|
return true; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return false; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Normalizes tabs in the specified string by indenting everything |
|
164
|
|
|
* back to the minimum tab distance. With the second parameter, |
|
165
|
|
|
* tabs can optionally be converted to spaces as well (recommended |
|
166
|
|
|
* for HTML output). |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $string |
|
169
|
|
|
* @param boolean $tabs2spaces |
|
170
|
|
|
* @return string |
|
171
|
|
|
*/ |
|
172
|
|
|
public static function normalizeTabs(string $string, bool $tabs2spaces = false) : string |
|
173
|
|
|
{ |
|
174
|
|
|
$normalizer = new ConvertHelper_TabsNormalizer(); |
|
175
|
|
|
$normalizer->convertTabsToSpaces($tabs2spaces); |
|
176
|
|
|
|
|
177
|
|
|
return $normalizer->normalize($string); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Converts tabs to spaces in the specified string. |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $string |
|
184
|
|
|
* @param int $tabSize The amount of spaces per tab. |
|
185
|
|
|
* @return string |
|
186
|
|
|
*/ |
|
187
|
|
|
public static function tabs2spaces(string $string, int $tabSize=4) : string |
|
188
|
|
|
{ |
|
189
|
|
|
return str_replace("\t", str_repeat(' ', $tabSize), $string); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Converts spaces to tabs in the specified string. |
|
194
|
|
|
* |
|
195
|
|
|
* @param string $string |
|
196
|
|
|
* @param int $tabSize The amount of spaces per tab in the source string. |
|
197
|
|
|
* @return string |
|
198
|
|
|
*/ |
|
199
|
|
|
public static function spaces2tabs(string $string, int $tabSize=4) : string |
|
200
|
|
|
{ |
|
201
|
|
|
return str_replace(str_repeat(' ', $tabSize), "\t", $string); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Makes all hidden characters visible in the target string, |
|
206
|
|
|
* from spaces to control characters. |
|
207
|
|
|
* |
|
208
|
|
|
* @param string $string |
|
209
|
|
|
* @return string |
|
210
|
|
|
*/ |
|
211
|
|
|
public static function hidden2visible(string $string) : string |
|
212
|
|
|
{ |
|
213
|
|
|
$converter = new ConvertHelper_HiddenConverter(); |
|
214
|
|
|
|
|
215
|
|
|
return $converter->convert($string); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* UTF8-safe wordwrap method: works like the regular wordwrap |
|
220
|
|
|
* PHP function but compatible with UTF8. Otherwise the lengths |
|
221
|
|
|
* are not calculated correctly. |
|
222
|
|
|
* |
|
223
|
|
|
* @param string $str |
|
224
|
|
|
* @param int $width |
|
225
|
|
|
* @param string $break |
|
226
|
|
|
* @param bool $cut |
|
227
|
|
|
* @return string |
|
228
|
|
|
*/ |
|
229
|
|
|
public static function wordwrap(string $str, int $width = 75, string $break = "\n", bool $cut = false) : string |
|
230
|
|
|
{ |
|
231
|
|
|
$wrapper = new ConvertHelper_WordWrapper(); |
|
232
|
|
|
|
|
233
|
|
|
return $wrapper |
|
234
|
|
|
->setLineWidth($width) |
|
235
|
|
|
->setBreakCharacter($break) |
|
236
|
|
|
->setCuttingEnabled($cut) |
|
237
|
|
|
->wrapText($str); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Transliterates a string. |
|
242
|
|
|
* |
|
243
|
|
|
* @param string $string |
|
244
|
|
|
* @param string $spaceChar |
|
245
|
|
|
* @param bool $lowercase |
|
246
|
|
|
* @return string |
|
247
|
|
|
*/ |
|
248
|
|
|
public static function transliterate(string $string, string $spaceChar = '-', bool $lowercase = true) : string |
|
249
|
|
|
{ |
|
250
|
|
|
$translit = new Transliteration(); |
|
251
|
|
|
$translit->setSpaceReplacement($spaceChar); |
|
252
|
|
|
if ($lowercase) { |
|
253
|
|
|
$translit->setLowercase(); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
return $translit->convert($string); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Cuts a text to the specified length if it is longer than the |
|
261
|
|
|
* target length. Appends a text to signify it has been cut at |
|
262
|
|
|
* the end of the string. |
|
263
|
|
|
* |
|
264
|
|
|
* @param string $text |
|
265
|
|
|
* @param int $targetLength |
|
266
|
|
|
* @param string $append |
|
267
|
|
|
* @return string |
|
268
|
|
|
*/ |
|
269
|
|
|
public static function cutText(string $text, int $targetLength, string $append = '...') : string |
|
270
|
|
|
{ |
|
271
|
|
|
$length = mb_strlen($text); |
|
272
|
|
|
if ($length <= $targetLength) { |
|
273
|
|
|
return $text; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
$text = trim(mb_substr($text, 0, $targetLength)) . $append; |
|
277
|
|
|
|
|
278
|
|
|
return $text; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Like explode, but trims all entries, and removes |
|
283
|
|
|
* empty entries from the resulting array. |
|
284
|
|
|
* |
|
285
|
|
|
* @param string $delimiter |
|
286
|
|
|
* @param string $string |
|
287
|
|
|
* @return string[] |
|
288
|
|
|
*/ |
|
289
|
|
|
public static function explodeTrim(string $delimiter, string $string) : array |
|
290
|
|
|
{ |
|
291
|
|
|
if(empty($string) || empty($delimiter)) { |
|
292
|
|
|
return array(); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
$tokens = explode($delimiter, $string); |
|
296
|
|
|
$tokens = array_map('trim', $tokens); |
|
297
|
|
|
|
|
298
|
|
|
$keep = array(); |
|
299
|
|
|
foreach($tokens as $token) { |
|
300
|
|
|
if($token !== '') { |
|
301
|
|
|
$keep[] = $token; |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
return $keep; |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
|