This repository was archived by the owner on Sep 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathText.php
More file actions
474 lines (419 loc) · 15.4 KB
/
Text.php
File metadata and controls
474 lines (419 loc) · 15.4 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<?php
/**
* @package Flextype Components
*
* @author Sergey Romanenko <awilum@yandex.ru>
* @link http://components.flextype.org
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flextype\Component\Text;
class Text
{
/**
* Translit function to convert text from one script to another.
*
* echo Text::translitIt('Привет');
*
* @param string $str string
* @return string $str
*/
public static function translitIt(string $str) : string
{
$patern = [
// Latin
'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'AE', 'Ç'=>'C',
'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I',
'Ð'=>'D', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ő'=>'O',
'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ű'=>'U', 'Ý'=>'Y', 'Þ'=>'TH',
'ß'=>'ss',
'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'ae', 'ç'=>'c',
'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i',
'ð'=>'d', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ő'=>'o',
'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ü'=>'u', 'ű'=>'u', 'ý'=>'y', 'þ'=>'th',
'ÿ'=>'y',
// Latin symbols
'©'=>'(c)',
// Greek
'Α'=>'A', 'Β'=>'B', 'Γ'=>'G', 'Δ'=>'D', 'Ε'=>'E', 'Ζ'=>'Z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', 'Ф'=>'F', 'Х'=>'H', 'Ц'=>'C',
'Ч'=>'Ch', 'Ш'=>'Sh', 'Щ'=>'Sh', 'Ъ'=>'', 'Ы'=>'Y', 'Ь'=>'', 'Э'=>'E', 'Ю'=>'Yu',
'Я'=>'Ya',
'а'=>'a', 'б'=>'b', 'в'=>'v', 'г'=>'g', 'д'=>'d', 'е'=>'e', 'ё'=>'yo', 'ж'=>'zh',
'з'=>'z', 'и'=>'i', 'й'=>'j', 'к'=>'k', 'л'=>'l', 'м'=>'m', 'н'=>'n', 'о'=>'o',
'п'=>'p', 'р'=>'r', 'с'=>'s', 'т'=>'t', 'у'=>'u', 'ф'=>'f', 'х'=>'h', 'ц'=>'c',
'ч'=>'ch', 'ш'=>'sh', 'щ'=>'sh', 'ъ'=>'', 'ы'=>'y', 'ь'=>'', 'э'=>'e', 'ю'=>'yu',
'я'=>'ya',
// Bulgarian
'А'=>'A', 'Б'=>'B', 'В'=>'V', 'Г'=>'G', 'Д'=>'D', 'Е'=>'E', 'Ж'=>'Zh', 'З'=>'Z',
'И'=>'I', 'Й'=>'J', 'К'=>'K', 'Л'=>'L', 'М'=>'M', 'Н'=>'N', 'О'=>'O', 'П'=>'P',
'Р'=>'R', 'С'=>'S', 'Т'=>'T', 'У'=>'U', 'Ф'=>'F', 'Х'=>'H', 'Ц'=>'C', 'Ч'=>'Ch',
'Ш'=>'Sh', 'Щ'=>'Sh', 'Ъ'=>'', 'Ь'=>'J','Ю'=>'Yu','Я'=>'Ya',
'а'=>'a', 'б'=>'b', 'в'=>'v', 'г'=>'g', 'д'=>'d', 'е'=>'e', 'ж'=>'zh','з'=>'z',
'и'=>'i', 'й'=>'j', 'к'=>'k', 'л'=>'l', 'м'=>'m', 'н'=>'n', 'о'=>'o','п'=>'p',
'р'=>'r', 'с'=>'s', 'т'=>'t', 'у'=>'u', 'ф'=>'f', 'х'=>'h', 'ц'=>'c', 'ч'=>'ch',
'ш'=>'sh', 'щ'=>'sh', 'ъ'=>'', 'ь'=>'j', 'ю'=>'yu', 'я'=>'ya',
// Ukrainian
'Є'=>'Ye', 'І'=>'I', 'Ї'=>'Yi', 'Ґ'=>'G',
'є'=>'ye', 'і'=>'i', 'ї'=>'yi', 'ґ'=>'g',
// Czech
'Č'=>'C', 'Ď'=>'D', 'Ě'=>'E', 'Ň'=>'N', 'Ř'=>'R', 'Š'=>'S', 'Ť'=>'T', 'Ů'=>'U',
'Ž'=>'Z',
'č'=>'c', 'ď'=>'d', 'ě'=>'e', 'ň'=>'n', 'ř'=>'r', 'š'=>'s', 'ť'=>'t', 'ů'=>'u',
'ž'=>'z',
// Polish
'Ą'=>'A', 'Ć'=>'C', 'Ę'=>'e', 'Ł'=>'L', 'Ń'=>'N', 'Ó'=>'o', 'Ś'=>'S', 'Ź'=>'Z',
'Ż'=>'Z',
'ą'=>'a', 'ć'=>'c', 'ę'=>'e', 'ł'=>'l', 'ń'=>'n', 'ó'=>'o', 'ś'=>'s', 'ź'=>'z',
'ż'=>'z',
// Latvian
'Ā'=>'A', 'Č'=>'C', 'Ē'=>'E', 'Ģ'=>'G', 'Ī'=>'i', 'Ķ'=>'k', 'Ļ'=>'L', 'Ņ'=>'N',
'Š'=>'S', 'Ū'=>'u', 'Ž'=>'Z',
'ā'=>'a', 'č'=>'c', 'ē'=>'e', 'ģ'=>'g', 'ī'=>'i', 'ķ'=>'k', 'ļ'=>'l', 'ņ'=>'n',
'š'=>'s', 'ū'=>'u', 'ž'=>'z'
];
return strtr($str, $patern);
}
/**
* Removes any leading and traling slashes from a string
*
* echo Text::trimSlashes('some text here/');
*
* @param string $str String with slashes
* @return string
*/
public static function trimSlashes(string $str) : string
{
return trim($str, '/');
}
/**
* Reduces multiple slashes in a string to single slashes.
*
* echo Text::reduceSlashes('some//text//here');
*
* @param string $str String or array of strings with slashes
* @return string
*/
public static function reduceSlashes(string $str) : string
{
return preg_replace('#(?<!:)//+#', '/', $str);
}
/**
* Removes single and double quotes from a string
*
* echo Text::stripQuotes('some "text" here');
*
* @param string $str String with single and double quotes
* @return string
*/
public static function stripQuotes(string $str) : string
{
return str_replace(array('"', "'"), '', $str);
}
/**
* Convert single and double quotes to entities
*
* echo Text::quotesToEntities('some "text" here');
*
* @param string $str String with single and double quotes
* @return string
*/
public static function quotesToEntities(string $str) : string
{
return str_replace(array("\'", "\"", "'", '"'), array("'", """, "'", """), $str);
}
/**
* Creates a random string of characters
*
* echo Text::random();
*
* @param string $type The type of string. Default is 'alnum'
* @param integer $length The number of characters. Default is 16
* @return string
*/
public static function random(string $type = 'alnum', int $length = 16) : string
{
switch ($type) {
case 'basic':
return mt_rand();
break;
default:
case 'alnum':
case 'numeric':
case 'nozero':
case 'alpha':
case 'distinct':
case 'hexdec':
switch ($type) {
case 'alpha':
$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
default:
case 'alnum':
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 'numeric':
$pool = '0123456789';
break;
case 'nozero':
$pool = '123456789';
break;
case 'distinct':
$pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
break;
case 'hexdec':
$pool = '0123456789abcdef';
break;
}
$str = '';
for ($i=0; $i < $length; $i++) {
$str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
}
return $str;
break;
case 'unique':
return md5(uniqid(mt_rand()));
break;
case 'sha1' :
return sha1(uniqid(mt_rand(), true));
break;
}
}
/**
* Add's _1 to a string or increment the ending number to allow _2, _3, etc
*
* $str = Text::increment($str);
*
* @param string $str String to increment
* @param int $first Start with
* @param string $separator Separator
* @return string
*/
public static function increment(string $str, int $first = 1, string $separator = '_') : string
{
preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match);
return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first;
}
/**
* Cut string
*
* echo Text::cut('Some text here', 5);
*
* @param string $str Input string
* @param int $length Length after cut
* @param string $cut_msg Message after cut string
* @return string
*/
public static function cut(string $str, int $length, string $cut_msg = null) : string
{
if (isset($cut_msg)) $msg = $cut_msg; else $msg = '...';
return function_exists('mb_substr') ? mb_substr($str, 0, $length, 'utf-8') . $msg : substr($str, 0, $length) . $msg;
}
/**
* Lowercase
*
* echo Text::lowercase('Some text here');
*
* @param string $str String
* @return string
*/
public static function lowercase(string $str) : string
{
return function_exists('mb_strtolower') ? mb_strtolower($str, 'utf-8') : strtolower($str);
}
/**
* Uppercase
*
* echo Text::uppercase('some text here');
*
* @param string $str String
* @return string
*/
public static function uppercase(string $str) : string
{
return function_exists('mb_strtoupper') ? mb_strtoupper($str, 'utf-8') : strtoupper($str);
}
/**
* Get length
*
* echo Text::length('Some text here');
*
* @param string $str String
* @return string
*/
public static function length(string $str) : string
{
return function_exists('mb_strlen') ? mb_strlen($str, 'utf-8') : strlen($str);
}
/**
* Create a lorem ipsum text
*
* echo Text::lorem(2);
*
* @param int $num Count
* @return string
*/
public static function lorem(int $num = 1) : string
{
return str_repeat('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.', $num);
}
/**
* Extract the last `$num` characters from a string.
*
* echo Text::right('Some text here', 4);
*
* @param string $str The string to extract the characters from.
* @param int $num The number of characters to extract.
* @return string
*/
public static function right(string $str, int $num) : string
{
return substr($str, Text::length($str)-$num, $num);
}
/**
* Extract the first `$num` characters from a string.
*
* echo Text::left('Some text here', 4);
*
* @param string $str The string to extract the characters from.
* @param int $num The number of characters to extract.
* @return string
*/
public static function left(string $str, int $num) : string
{
return substr($str, 0, $num);
}
/**
* Replaces newline with <br> or <br />.
*
* echo Text::nl2br("Some \n text \n here");
*
* @param string $str The input string
* @param bool $xhtml Xhtml or not
* @return string
*/
public static function nl2br(string $str, bool $xhtml = true) : string
{
return str_replace(array("\r\n", "\n\r", "\n", "\r"), (($xhtml) ? '<br />' : '<br>'), $str);
}
/**
* Replaces <br> and <br /> with newline.
*
* echo Text::br2nl("Some <br /> text <br /> here");
*
* @param string $str The input string
* @return string
*/
public static function br2nl(string $str) : string
{
return str_replace(array('<br>', '<br/>', '<br />'), "\n", $str);
}
/**
* Converts & to &.
*
* echo Text::ampEncode("M&CMS");
*
* @param string $str The input string
* @return string
*/
public static function ampEncode(string $str) : string
{
return str_replace('&', '&', $str);
}
/**
* Converts & to &.
*
* echo Text::ampEncode("M&CMS");
*
* @param string $str The input string
* @return string
*/
public static function ampDecode(string $str) : string
{
return str_replace('&', '&', $str);
}
/**
* Convert plain text to html
*
* echo Text::toHtml('test');
*
* @param string $str String
* @return string
*/
public static function toHtml(string $str) : string
{
return html_entity_decode($str, ENT_QUOTES, 'utf-8');
}
/**
* Create safe string. Use to create safe usernames or filenames.
*
* $safe_string = Text::safeString('hello world');
*
* @param string $str String
* @param string $delimiter String delimiter
* @param boolean $lowercase String Lowercase
* @return string
*/
public static function safeString(string $str, string $delimiter = '-', bool $lowercase = false) : string
{
// Remove tags
$str = filter_var($str, FILTER_SANITIZE_STRING);
// Decode all entities to their simpler forms
$str = html_entity_decode($str, ENT_QUOTES, 'UTF-8');
// Reserved characters (RFC 3986)
$reserved_characters = array(
'/', '?', ':', '@', '#', '[', ']',
'!', '$', '&', '\'', '(', ')', '*',
'+', ',', ';', '='
);
// Remove reserved characters
$str = str_replace($reserved_characters, ' ', $str);
// Set locale to en_US.UTF8
setlocale(LC_ALL, 'en_US.UTF8');
// Translit ua,ru => latin
$str = Text::translitIt($str);
// Convert string
$str = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
// Remove characters
$str = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $str);
if ($delimiter != null) {
$str = preg_replace("/[\/_|+ -]+/", $delimiter, $str);
$str = trim($str, $delimiter);
}
// Lowercase
if ($lowercase === true) {
$str = Text::lowercase($str);
}
// Return safe name
return $str;
}
/**
* Encrypt string
*
* $encrypt_string = Text::encryptString('password', 'string_salt');
*
* @param string $str String to encrypt
* @param string $str_salt String salt
* @return string
*/
public static function encryptPassword($str, $str_salt = 'string_salt')
{
return md5(md5(trim($str) . $str_salt));
}
}