This repository was archived by the owner on Dec 6, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathString.php
More file actions
841 lines (737 loc) · 22.1 KB
/
Copy pathString.php
File metadata and controls
841 lines (737 loc) · 22.1 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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
<?php
/**
* string functions wrapper and util functions to String
*/
class EtuDev_Util_String
{
static protected $rewrite_string_to_void = array("'", '<br>', '<br/>', '"');
static protected $rewrite_words_to_dash = array(
'-be-',
'-had-',
'-it-',
'-was-',
'-its-',
'-of-',
'-we-',
'-on-',
'-but-',
'-by-',
'-her-',
'-or-',
'-an-',
'-can-',
'-mr-',
'-the-',
'-who-',
'-any-',
'-if-',
'-mrs-',
'-out-',
'-and-',
'-corp-',
'-in-',
'-are-',
'-inc-',
'-mz-',
'-s-',
'-as-',
'-for-',
'-so-',
'-this-',
'-up-',
'-at-',
'-from-',
'-is-',
'-says-',
'-to-',
);
/**
* old function rewrite
*
* @param string $str
*
* @return string
*/
static public function rewriteString($str)
{
return str_replace(
'--',
'-',
str_replace(
static::$rewrite_words_to_dash,
'-',
preg_replace(
'/[\s]/',
'-',
preg_replace(
"/[^a-zA-Z0-9&]+/",
"-",
preg_replace(
'/[[:punct:]]/',
'-',
preg_replace(
'/[%\?"¿\.]/',
'',
str_replace(
static::$rewrite_string_to_void,
'',
preg_replace('@\pM@u', '', Normalizer::normalize($str, Normalizer::FORM_D))
)
)
)
)
)
)
);
}
static public function getRandomStringOnlyChar($length, $filter = array())
{
return self::getRandomString($length, array_merge(range(1, 9), $filter));
}
static public function getRandomStringOnlyNum($length, $filter = array())
{
return self::getRandomString($length, array_merge(range('A', 'Z'), $filter));
}
static public function getRandomString($length, $filter = array())
{
$array = array_diff(array_merge(range(1, 9), range('A', 'Z')), $filter);
shuffle($array);
return implode('', array_slice($array, 0, $length));
}
static public function toCamelCase($str, $capitaliseFirstChar = false)
{
if ($capitaliseFirstChar) {
$str[0] = strtoupper($str[0]);
}
return preg_replace('/_([a-z])/e', "strtoupper('\\1')", $str);
}
static public function trimToNearestWordWithDefaultAllowTags($content, $chars, $append = '...')
{
return static::trimToNearestWord($content, $chars, $append, '<br><strong><b>');
}
/**
* @static
*
* @param string $content
* @param int $chars
* @param string $append
* @param null $allowedTags argument to strip_tags (ie: '<br><p>')
* @param bool $repairHTML if true calls repairHTML() to the result
*
* @return string
*/
static public function trimToNearestWord($content, $chars, $append = '...', $allowedTags = null, $repairHTML = true)
{
if (static::strlen($content) > $chars) {
$content = str_replace(' ', ' ', $content); //html space to space
$content = str_replace("\n", ' ', $content); //new line to space
$content = strip_tags(trim($content), $allowedTags);
$c = static::substr($content, 0, $chars, false);
$content = preg_replace('/\s+?(\S+)?$/', '', $c);
$content = trim($content) . $append;
if ($repairHTML) {
$content = static::repairHTML($content);
}
return $content;
}
return $content;
}
static public function multipleSpacesToSingleSpace($string)
{
return preg_replace('/[ \t\n\r]+/', ' ', $string);
}
static public function removeAllSpaces($string)
{
return preg_replace('/[ \t\n\r]+/', '', $string);
}
static public function removeAllButNumbers($string, $allowPlus = false)
{
$patron = $allowPlus ? '/[^0-9+]+/' : '/[^0-9]+/';
return preg_replace($patron, '', $string);
}
/**
* @static
*
* @param string $string
* @param string|array $needle string o array of strings => si es array, devuelve true si empieza por alguno de ellos, false si por ninguno
*
* @return bool
*/
static public function beginsWith($string, $needle)
{
if ($needle) {
if (!is_array($needle)) {
$needle = array($needle);
}
foreach ($needle as $n) {
$s = self::substr($string, 0, self::strlen($n), false);
if ($s === (string) $n) {
return true;
}
}
return false;
}
return true;
}
/**
* @static
*
* @param string $haystack
* @param string|array $needle string o array of strings => si es array, devuelve true si termina por alguno de ellos, false si por ninguno
*
* @return bool
*/
static public function endsWith($haystack, $needle)
{
if ($needle) {
if (!is_array($needle)) {
$needle = array($needle);
}
foreach ($needle as $n) {
$length = self::strlen($n);
$start = $length * -1; //negative
$s = self::substr($haystack, $start, $length);
if ($s === (string) $n) {
return true;
}
}
return false;
}
return true;
}
static public function stripos($haystack, $needle, $offset = null)
{
return mb_stripos($haystack, $needle, $offset, 'UTF-8');
}
static public function strpos($haystack, $needle, $offset = null)
{
return mb_strpos($haystack, $needle, $offset, 'UTF-8');
}
static public function substr($string, $start, $length = null, $repair = true)
{
if ($length) {
$str = mb_substr($string, $start, $length, 'UTF-8');
} else {
$str = mb_substr($string, $start, mb_strlen($string, 'UTF-8') + 1, 'UTF-8');
}
return $repair ? self::repairHTML($str) : $str;
}
/**
* returns mb_strlen
* @static
*
* @param string $str
*
* @return int
*/
static public function strlen($str)
{
return mb_strlen($str, 'utf-8');
}
static public $htmlEntitiesDefaultOptions = array('charset' => 'UTF-8', 'quote_style' => ENT_COMPAT, 'double_encode' => true);
static public function htmlentities($string, $options = array())
{
$ops = array_merge(self::$htmlEntitiesDefaultOptions, $options);
return htmlentities($string, $ops['quote_style'], $ops['charset'], $ops['double_encode']);
}
/**
* replace chars with HTML entities and those with XML entities
*
* @static
*
* @param string $str
*
* @return string
* @uses html2xmlEntities()
*/
static public function xmlEntities($str)
{
return self::html2xmlEntities(self::htmlentities($str));
}
/**
* replaces HTML entities with XML entities
*
* @static
*
* @param string $str
*
* @return string
*/
static public function html2xmlEntities($str)
{
$xml = array(
'"',
'&',
'&',
'<',
'>',
' ',
'¡',
'¢',
'£',
'¤',
'¥',
'¦',
'§',
'¨',
'©',
'ª',
'«',
'¬',
'­',
'®',
'¯',
'°',
'±',
'²',
'³',
'´',
'µ',
'¶',
'·',
'¸',
'¹',
'º',
'»',
'¼',
'½',
'¾',
'¿',
'À',
'Á',
'Â',
'Ã',
'Ä',
'Å',
'Æ',
'Ç',
'È',
'É',
'Ê',
'Ë',
'Ì',
'Í',
'Î',
'Ï',
'Ð',
'Ñ',
'Ò',
'Ó',
'Ô',
'Õ',
'Ö',
'×',
'Ø',
'Ù',
'Ú',
'Û',
'Ü',
'Ý',
'Þ',
'ß',
'à',
'á',
'â',
'ã',
'ä',
'å',
'æ',
'ç',
'è',
'é',
'ê',
'ë',
'ì',
'í',
'î',
'ï',
'ð',
'ñ',
'ò',
'ó',
'ô',
'õ',
'ö',
'÷',
'ø',
'ù',
'ú',
'û',
'ü',
'ý',
'þ',
'ÿ'
);
$html = array(
'"',
'&',
'&',
'<',
'>',
' ',
'¡',
'¢',
'£',
'¤',
'¥',
'¦',
'§',
'¨',
'©',
'ª',
'«',
'¬',
'­',
'®',
'¯',
'°',
'±',
'²',
'³',
'´',
'µ',
'¶',
'·',
'¸',
'¹',
'º',
'»',
'¼',
'½',
'¾',
'¿',
'À',
'Á',
'Â',
'Ã',
'Ä',
'Å',
'Æ',
'Ç',
'È',
'É',
'Ê',
'Ë',
'Ì',
'Í',
'Î',
'Ï',
'Ð',
'Ñ',
'Ò',
'Ó',
'Ô',
'Õ',
'Ö',
'×',
'Ø',
'Ù',
'Ú',
'Û',
'Ü',
'Ý',
'Þ',
'ß',
'à',
'á',
'â',
'ã',
'ä',
'å',
'æ',
'ç',
'è',
'é',
'ê',
'ë',
'ì',
'í',
'î',
'ï',
'ð',
'ñ',
'ò',
'ó',
'ô',
'õ',
'ö',
'÷',
'ø',
'ù',
'ú',
'û',
'ü',
'ý',
'þ',
'ÿ'
);
$str = str_replace($html, $xml, $str);
$str = str_ireplace($html, $xml, $str);
return $str;
}
/**
*
* @param string $str contains the complete raw name string
* @param array $a_char is an array containing the characters we use as separators for capitalization. If you don't pass anything, there are three in there as default.
*/
static public function nameize($str, $a_char = array("'", "-", " "))
{
$string = mb_convert_case(mb_convert_case($str, MB_CASE_LOWER, 'UTF-8'), MB_CASE_TITLE, 'UTF-8');
$string = preg_replace_callback(
"/( [ a-zA-Z]{1}')([a-zA-Z0-9]{1})/s",
create_function('$matches', 'return $matches[1].mb_convert_case($matches[2], MB_CASE_UPPER, "UTF-8");'),
$string
);
return $string;
}
static public function normal($string, $acceptGSMValidNotAccents = false)
{
$ts = array(
"[À-Å]",
"[È-Ë]",
"[Ì-Ï]",
"[Ò-Ö]",
"[Ù-Ü]",
"[Ý-Þ]",
"[à-å]",
"[è-ë]",
"[ì-ï]",
"ð",
"[ò-ö]",
"÷",
"·",
"[ù-ü]",
"[ý-ÿ]"
);
$tn = array("A", "E", "I", "O", "U", "Y", "a", "e", "i", "d", "o", "/", ".", "u", "y");
if (!$acceptGSMValidNotAccents) {
$ts[] = "Æ";
$tn[] = 'AE';
$ts[] = "ß";
$tn[] = 'ss';
$ts[] = "Ç";
$tn[] = 'C';
$ts[] = "Ð";
$tn[] = 'D';
$ts[] = "Ñ";
$tn[] = 'N';
$ts[] = "Ø";
$tn[] = 'O';
$ts[] = "×";
$tn[] = 'x';
$ts[] = "æ";
$tn[] = 'ae';
$ts[] = "ç";
$tn[] = 'c';
$ts[] = "ñ";
$tn[] = 'n';
$ts[] = "ø";
$tn[] = 'o';
}
foreach ($ts as $k => $pattern) {
$replace = $tn[$k];
$string = mb_ereg_replace($pattern, $replace, $string);
}
return $string;
}
static public function ucfirst($string, $e = 'utf-8')
{
if (function_exists('mb_strtoupper') && function_exists('mb_substr') && !empty($string)) {
$string = mb_strtolower($string, $e);
$upper = mb_strtoupper($string, $e);
preg_match('#(.)#us', $upper, $matches);
$string = $matches[1] . mb_substr($string, 1, mb_strlen($string, $e), $e);
} else {
$string = ucfirst($string);
}
return $string;
}
static public function strtolower($str, $e = 'utf-8')
{
return function_exists('mb_strtolower') ? mb_strtolower((string) $str, $e) : strtolower((string) $str);
}
static public function strtoupper($str, $e = 'utf-8')
{
return function_exists('mb_strtoupper') ? mb_strtoupper((string) $str, $e) : strtoupper((string) $str);
}
static public function sentenceCase($string)
{
$sentences = preg_split('/([.?!]+)/', $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$new_string = '';
foreach ($sentences as $key => $sentence) {
$new_string .= ($key & 1) == 0 ? self::ucfirst(self::strtolower(trim($sentence))) : $sentence . ' ';
}
return trim($new_string);
}
/**
* given a string (or an array of strings), it changes the <br/> to \n
*
* @param string|array $p
* @param string $removeChar
*
* @return string|array
*/
static public function removeBR($p, $removeChar = "\n")
{
$returns = array("<br>", "<br />", "<br/>");
$c = str_ireplace($returns, $removeChar, $p);
return $c;
}
static public function encryptDecrypt($Str_Message)
{
//Function : encrypt/decrypt a string message v.1.0 without a known key
//Author : Aitor Solozabal Merino (spain)
//Email : aitor-3@euskalnet.net
//Date : 01-04-2005
$Len_Str_Message = strlen($Str_Message);
$Str_Encrypted_Message = "";
for ($Position = 0; $Position < $Len_Str_Message; $Position++) {
// long code of the function to explain the algoritm
//this function can be tailored by the programmer modifyng the formula
//to calculate the key to use for every character in the string.
$Key_To_Use = (($Len_Str_Message + $Position) + 1); // (+5 or *3 or ^2)
//after that we need a module division because can´t be greater than 255
$Key_To_Use = (255 + $Key_To_Use) % 255;
$Byte_To_Be_Encrypted = substr($Str_Message, $Position, 1);
$Ascii_Num_Byte_To_Encrypt = ord($Byte_To_Be_Encrypted);
$Xored_Byte = $Ascii_Num_Byte_To_Encrypt ^ $Key_To_Use; //xor operation
$Encrypted_Byte = chr($Xored_Byte);
$Str_Encrypted_Message .= $Encrypted_Byte;
//short code of the function once explained
//$str_encrypted_message .= chr((ord(substr($str_message, $position, 1))) ^ ((255+(($len_str_message+$position)+1)) % 255));
}
return $Str_Encrypted_Message;
}
static public function encode($ss, $ntime = null)
{
if (!$ntime) {
$ntime = self::ENCODE_LENGTH;
}
for ($i = 0; $i < $ntime; $i++) {
$ss = base64_encode($ss);
}
return $ss;
}
static public function decode($ss, $ntime = null)
{
if (!$ntime) {
$ntime = self::ENCODE_LENGTH;
}
for ($i = 0; $i < $ntime; $i++) {
$ss = base64_decode($ss);
}
return $ss;
}
const ENCODE_LENGTH = 5;
static public function encode_encrypted($string)
{
return self::encode(self::encryptDecrypt($string), self::ENCODE_LENGTH);
}
static public function decode_encrypted($string)
{
return self::encryptDecrypt(self::decode($string, self::ENCODE_LENGTH));
}
const ALLOWED_TAGS_STANDARD_LIST = 'u,i,br,b,a,strong';
static public function getAllowedTagsStandard()
{
return explode(',', self::ALLOWED_TAGS_STANDARD_LIST);
}
/**
* Return the first $amount chars of a string without splitting words unless $forced = true
*
* @param string $string string to cut
* @param int $amount characters to return
* @param boolean $includeLastWord include or not the last word
* @param boolean $forced force to return just 100 chars, splitting words
*/
static public function getChars($string, $amount, $includeLastWord = true, $forced = false)
{
if ($forced) {
return self::substr($string, 0, $amount);
} elseif ($includeLastWord) {
if (strpos($string, ' ', $amount) !== false) {
return self::substr($string, 0, strpos($string, ' ', $amount));
} else {
return $string;
}
} else {
return self::substr($string, 0, strrpos($string, ' ', $amount));
}
}
public static function htmlspecialchars($string, $quote_style = ENT_COMPAT, $charset = 'UTF-8', $double_encode = true)
{
return htmlspecialchars($string, $quote_style, $charset, $double_encode);
}
public static function removeSpacesDotsMinus($string)
{
return preg_replace('/[\s\.\-]/', '', $string);
}
public static function contains($string, $substr, $case_sensitive = true)
{
if ($case_sensitive) {
return (strpos($string, $substr) !== false);
} else {
return (stripos($string, $substr) !== false);
}
}
public static function firstToken($string, $separator = ',')
{
return substr($string, 0, strpos($string, $separator));
}
public static function isUtf8($string)
{
return (md5(iconv('utf-8', 'utf-8', $string)) == md5($string));
}
public static function lastToken($string, $token = ',')
{
return substr($string, strrpos($string, $token) + 1);
}
public static function hex2rbg($hex)
{
$hex = str_replace('#', '', $hex);
$len = strlen($hex);
$r = substr($hex, 0, $len / 3);
$g = substr($hex, $len / 3, ($len / 3) * 2);
$b = substr($hex, ($len / 3) * 2);
if ($len == 3) {
$r .= $r;
$g .= $g;
$b .= $b;
}
return array('r' => hexdec($r), 'g' => hexdec($g), 'b' => hexdec($b));
}
public static function similarity($string1, $string2)
{
$nonSense = array(',', '.', '_', '#', '¿', '?', '¡', '!', '"');
$p = null;
similar_text(str_replace($nonSense, '', self::strtolower($string1)), str_replace($nonSense, '', self::strtolower($string2)), $p);
return $p;
}
public static $repairHTMLTidyOptions = array(
'wrap' => 0,
'doctype' => 'omit',
'tidy-mark' => '0',
'wrap-sections' => '0',
'char-encoding' => 'utf8',
'input-encoding' => 'utf8',
'show-body-only' => 1
);
public static function repairHTML($string, $encoding = 'utf8')
{
$tidy = new tidy();
return $tidy->repairString($string, self::$repairHTMLTidyOptions, $encoding);
}
public static function stringContainsUrl($x)
{
return (preg_match('#(http|https|ftp)://([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/?#i', $x)) > 0;
}
static public function cleanFormOutput($mixed)
{
if (is_array($mixed)) {
return array_map(array(__CLASS__, __FUNCTION__), $mixed);
} else {
// Supress errors since some charsets not supported
return str_replace('&', '&', @htmlspecialchars($mixed, ENT_QUOTES));
}
}
}