1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\Arr; |
4
|
|
|
|
5
|
|
|
if (! function_exists('str_trimmer')) { |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Retorna un string sin espacios en blancos. |
9
|
|
|
* |
10
|
|
|
* @param mixed $value |
11
|
|
|
* @return mixed |
12
|
|
|
*/ |
13
|
|
|
function str_trimmer($value) |
14
|
|
|
{ |
15
|
|
|
return is_string($value) ? preg_replace('/\s+/', ' ', trim($value)) : $value; |
16
|
|
|
} |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
if (! function_exists('filter_nullables')) { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Verifica si tiene un valor válido, incluye zero. |
23
|
|
|
* |
24
|
|
|
* @param mixed $value |
25
|
|
|
* @return bool |
26
|
|
|
*/ |
27
|
|
|
function filter_nullables($value): bool |
28
|
|
|
{ |
29
|
|
|
return ($value !== null && $value !== false && $value !== ''); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (! function_exists('array_filter_empty')) { |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Filtra el array eliminando valores 'vacios'. |
37
|
|
|
* |
38
|
|
|
* @param array $array |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
function array_filter_empty($array): array |
42
|
|
|
{ |
43
|
|
|
$array = array_map('str_trimmer', $array); |
44
|
|
|
$array = array_filter($array, 'filter_nullables'); |
45
|
|
|
|
46
|
|
|
return $array; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (! function_exists('array_filter_recursive')) { |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Filtra de forma recursiva un array. |
54
|
|
|
* |
55
|
|
|
* @link https://wpscholar.com/blog/filter-multidimensional-array-php/ |
56
|
|
|
* @param array $array |
57
|
|
|
* @param callable $callback |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
function array_filter_recursive(array $array, callable $callback = null): array |
61
|
|
|
{ |
62
|
|
|
$array = is_callable($callback) ? array_filter($array, $callback) : array_filter($array); |
63
|
|
|
foreach ($array as &$value) { |
64
|
|
|
if (is_array($value)) { |
65
|
|
|
$value = call_user_func(__FUNCTION__, $value, $callback); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $array; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (! function_exists('array_filler')) { |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Retorna un array con las array enviadas como parámetro. Sí no existe la key añade un valor por defecto. |
77
|
|
|
* |
78
|
|
|
* @param array $array |
79
|
|
|
* @param array $keys |
80
|
|
|
* @param mixed $default |
81
|
|
|
* @param array |
82
|
|
|
*/ |
83
|
|
|
function array_filler($array, $keys = [], $default = null): array |
84
|
|
|
{ |
85
|
|
|
$data = []; |
86
|
|
|
$keys = Arr::wrap($keys); |
87
|
|
|
|
88
|
|
|
foreach ($keys as $key) { |
89
|
|
|
$data[$key] = Arr::get($array, $key, $default); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $data; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (! function_exists('array_is_assoc')) { |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Verifica sí el array es asociativo. |
100
|
|
|
* |
101
|
|
|
* @param array $array |
102
|
|
|
* @param bool |
103
|
|
|
*/ |
104
|
|
|
function array_is_assoc($array): bool |
105
|
|
|
{ |
106
|
|
|
if (array() === $array) { |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return array_keys($array) !== range(0, count($array) - 1); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (! function_exists('array_keys_replace')) { |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Retorna un array cuyas keys serán reemplazada por los valores de otro array cuya keys sean iguales. |
118
|
|
|
* |
119
|
|
|
* @param array $array |
120
|
|
|
* @param array $replace |
121
|
|
|
* @param array |
122
|
|
|
*/ |
123
|
|
|
function array_keys_replace($array, $replace): array |
124
|
|
|
{ |
125
|
|
|
if (!array_is_assoc($array) or !array_is_assoc($replace)) { |
126
|
|
|
return $array; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$data = []; |
130
|
|
|
|
131
|
|
|
foreach ($array as $column => $value) { |
132
|
|
|
if (Arr::has($replace, $column)) { |
133
|
|
|
$key = Arr::get($replace, $column); |
134
|
|
|
|
135
|
|
|
$data[$key] = $value; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $data; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if (! function_exists('array_range')) { |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Obtiene un rango de números representado por un array asociativo. |
147
|
|
|
* |
148
|
|
|
* @param integer $from |
149
|
|
|
* @param integer $to |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
|
function array_range($from, $to): array |
153
|
|
|
{ |
154
|
|
|
return array_combine(range($from, $to), range($from, $to)); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
if (! function_exists('str_highlight')) { |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Resalta la palabra que haga match. |
163
|
|
|
* |
164
|
|
|
* @param string $string |
165
|
|
|
* @param string $word |
166
|
|
|
* @param string $class |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
function str_highlight($string, $word, $class = 'highlight'): string |
170
|
|
|
{ |
171
|
|
|
$string = str_sanitize($string); |
172
|
|
|
$word = str_sanitize($word); |
173
|
|
|
|
174
|
|
|
if (!$word) { |
175
|
|
|
return $string; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$replace = "<span class='{$class}'>$1</span>"; |
179
|
|
|
$pattern = preg_quote($word); |
180
|
|
|
|
181
|
|
|
return preg_replace("/($pattern)/i", $replace, $string); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if (! function_exists('str_sanitize')) { |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Remueve caracteres especiales de un string. |
189
|
|
|
* |
190
|
|
|
* @link https://www.experts-exchange.com/questions/23363984/Regular-expression-with-accented-characters.html |
191
|
|
|
* @param string $string |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
|
|
function str_sanitize($string): string |
195
|
|
|
{ |
196
|
|
|
$characters = [ |
197
|
|
|
'0' => ['°', '₀', '۰', '0'], |
198
|
|
|
'1' => ['¹', '₁', '۱', '1'], |
199
|
|
|
'2' => ['²', '₂', '۲', '2'], |
200
|
|
|
'3' => ['³', '₃', '۳', '3'], |
201
|
|
|
'4' => ['⁴', '₄', '۴', '٤', '4'], |
202
|
|
|
'5' => ['⁵', '₅', '۵', '٥', '5'], |
203
|
|
|
'6' => ['⁶', '₆', '۶', '٦', '6'], |
204
|
|
|
'7' => ['⁷', '₇', '۷', '7'], |
205
|
|
|
'8' => ['⁸', '₈', '۸', '8'], |
206
|
|
|
'9' => ['⁹', '₉', '۹', '9'], |
207
|
|
|
'a' => ['à', 'á', 'ả', 'ã', 'ạ', 'ă', 'ắ', 'ằ', 'ẳ', 'ẵ', 'ặ', 'â', 'ấ', 'ầ', 'ẩ', 'ẫ', 'ậ', 'ā', 'ą', 'å', 'α', 'ά', 'ἀ', 'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ', 'ἇ', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ', 'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ', 'ά', 'ᾰ', 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ', 'ᾶ', 'ᾷ', 'а', 'أ', 'အ', 'ာ', 'ါ', 'ǻ', 'ǎ', 'ª', 'ა', 'अ', 'ا', 'a', 'ä'], |
208
|
|
|
'b' => ['б', 'β', 'ب', 'ဗ', 'ბ', 'b'], |
209
|
|
|
'c' => ['ç', 'ć', 'č', 'ĉ', 'ċ', 'c'], |
210
|
|
|
'd' => ['ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ', 'д', 'δ', 'د', 'ض', 'ဍ', 'ဒ', 'დ', 'd'], |
211
|
|
|
'e' => ['é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ế', 'ề', 'ể', 'ễ', 'ệ', 'ë', 'ē', 'ę', 'ě', 'ĕ', 'ė', 'ε', 'έ', 'ἐ', 'ἑ', 'ἒ', 'ἓ', 'ἔ', 'ἕ', 'ὲ', 'έ', 'е', 'ё', 'э', 'є', 'ə', 'ဧ', 'ေ', 'ဲ', 'ე', 'ए', 'إ', 'ئ', 'e'], |
212
|
|
|
'f' => ['ф', 'φ', 'ف', 'ƒ', 'ფ', 'f'], |
213
|
|
|
'g' => ['ĝ', 'ğ', 'ġ', 'ģ', 'г', 'ґ', 'γ', 'ဂ', 'გ', 'گ', 'g'], |
214
|
|
|
'h' => ['ĥ', 'ħ', 'η', 'ή', 'ح', 'ه', 'ဟ', 'ှ', 'ჰ', 'h'], |
215
|
|
|
'i' => ['í', 'ì', 'ỉ', 'ĩ', 'ị', 'î', 'ï', 'ī', 'ĭ', 'į', 'ı', 'ι', 'ί', 'ϊ', 'ΐ', 'ἰ', 'ἱ', 'ἲ', 'ἳ', 'ἴ', 'ἵ', 'ἶ', 'ἷ', 'ὶ', 'ί', 'ῐ', 'ῑ', 'ῒ', 'ΐ', 'ῖ', 'ῗ', 'і', 'ї', 'и', 'ဣ', 'ိ', 'ီ', 'ည်', 'ǐ', 'ი', 'इ', 'ی', 'i'], |
216
|
|
|
'j' => ['ĵ', 'ј', 'Ј', 'ჯ', 'ج', 'j'], |
217
|
|
|
'k' => ['ķ', 'ĸ', 'к', 'κ', 'Ķ', 'ق', 'ك', 'က', 'კ', 'ქ', 'ک', 'k'], |
218
|
|
|
'l' => ['ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'л', 'λ', 'ل', 'လ', 'ლ', 'l'], |
219
|
|
|
'm' => ['м', 'μ', 'م', 'မ', 'მ', 'm'], |
220
|
|
|
'n' => ['ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν', 'н', 'ن', 'န', 'ნ', 'n'], |
221
|
|
|
'o' => ['ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ố', 'ồ', 'ổ', 'ỗ', 'ộ', 'ơ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ', 'ø', 'ō', 'ő', 'ŏ', 'ο', 'ὀ', 'ὁ', 'ὂ', 'ὃ', 'ὄ', 'ὅ', 'ὸ', 'ό', 'о', 'و', 'θ', 'ို', 'ǒ', 'ǿ', 'º', 'ო', 'ओ', 'o', 'ö'], |
222
|
|
|
'p' => ['п', 'π', 'ပ', 'პ', 'پ', 'p'], |
223
|
|
|
'q' => ['ყ', 'q'], |
224
|
|
|
'r' => ['ŕ', 'ř', 'ŗ', 'р', 'ρ', 'ر', 'რ', 'r'], |
225
|
|
|
's' => ['ś', 'š', 'ş', 'с', 'σ', 'ș', 'ς', 'س', 'ص', 'စ', 'ſ', 'ს', 's'], |
226
|
|
|
't' => ['ť', 'ţ', 'т', 'τ', 'ț', 'ت', 'ط', 'ဋ', 'တ', 'ŧ', 'თ', 'ტ', 't'], |
227
|
|
|
'u' => ['ú', 'ù', 'ủ', 'ũ', 'ụ', 'ư', 'ứ', 'ừ', 'ử', 'ữ', 'ự', 'û', 'ū', 'ů', 'ű', 'ŭ', 'ų', 'µ', 'у', 'ဉ', 'ု', 'ူ', 'ǔ', 'ǖ', 'ǘ', 'ǚ', 'ǜ', 'უ', 'उ', 'u', 'ў', 'ü'], |
228
|
|
|
'v' => ['в', 'ვ', 'ϐ', 'v'], |
229
|
|
|
'w' => ['ŵ', 'ω', 'ώ', 'ဝ', 'ွ', 'w'], |
230
|
|
|
'x' => ['χ', 'ξ', 'x'], |
231
|
|
|
'y' => ['ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ', 'ÿ', 'ŷ', 'й', 'ы', 'υ', 'ϋ', 'ύ', 'ΰ', 'ي', 'ယ', 'y'], |
232
|
|
|
'z' => ['ź', 'ž', 'ż', 'з', 'ζ', 'ز', 'ဇ', 'ზ', 'z'], |
233
|
|
|
'aa' => ['ع', 'आ', 'آ'], |
234
|
|
|
'ae' => ['æ', 'ǽ'], |
235
|
|
|
'ai' => ['ऐ'], |
236
|
|
|
'ch' => ['ч', 'ჩ', 'ჭ', 'چ'], |
237
|
|
|
'dj' => ['ђ', 'đ'], |
238
|
|
|
'dz' => ['џ', 'ძ'], |
239
|
|
|
'ei' => ['ऍ'], |
240
|
|
|
'gh' => ['غ', 'ღ'], |
241
|
|
|
'ii' => ['ई'], |
242
|
|
|
'ij' => ['ij'], |
243
|
|
|
'kh' => ['х', 'خ', 'ხ'], |
244
|
|
|
'lj' => ['љ'], |
245
|
|
|
'nj' => ['њ'], |
246
|
|
|
'oe' => ['ö', 'œ', 'ؤ'], |
247
|
|
|
'oi' => ['ऑ'], |
248
|
|
|
'oii' => ['ऒ'], |
249
|
|
|
'ps' => ['ψ'], |
250
|
|
|
'sh' => ['ш', 'შ', 'ش'], |
251
|
|
|
'shch' => ['щ'], |
252
|
|
|
'ss' => ['ß'], |
253
|
|
|
'sx' => ['ŝ'], |
254
|
|
|
'th' => ['þ', 'ϑ', 'ث', 'ذ', 'ظ'], |
255
|
|
|
'ts' => ['ц', 'ც', 'წ'], |
256
|
|
|
'ue' => ['ü'], |
257
|
|
|
'uu' => ['ऊ'], |
258
|
|
|
'ya' => ['я'], |
259
|
|
|
'yu' => ['ю'], |
260
|
|
|
'zh' => ['ж', 'ჟ', 'ژ'], |
261
|
|
|
'(c)' => ['©'], |
262
|
|
|
'A' => ['Á', 'À', 'Ả', 'Ã', 'Ạ', 'Ă', 'Ắ', 'Ằ', 'Ẳ', 'Ẵ', 'Ặ', 'Â', 'Ấ', 'Ầ', 'Ẩ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ῡ', 'Ὺ', 'Ύ', 'Ы', 'Й', 'Υ', 'Ϋ', 'Ŷ', 'Y'], |
287
|
|
|
'Z' => ['Ź', 'Ž', 'Ż', 'З', 'Ζ', 'Z'], |
288
|
|
|
'AE' => ['Æ', 'Ǽ'], |
289
|
|
|
'Ch' => ['Ч'], |
290
|
|
|
'Dj' => ['Ђ'], |
291
|
|
|
'Dz' => ['Џ'], |
292
|
|
|
'Gx' => ['Ĝ'], |
293
|
|
|
'Hx' => ['Ĥ'], |
294
|
|
|
'Ij' => ['IJ'], |
295
|
|
|
'Jx' => ['Ĵ'], |
296
|
|
|
'Kh' => ['Х'], |
297
|
|
|
'Lj' => ['Љ'], |
298
|
|
|
'Nj' => ['Њ'], |
299
|
|
|
'Oe' => ['Œ'], |
300
|
|
|
'Ps' => ['Ψ'], |
301
|
|
|
'Sh' => ['Ш'], |
302
|
|
|
'Shch' => ['Щ'], |
303
|
|
|
'Ss' => ['ẞ'], |
304
|
|
|
'Th' => ['Þ'], |
305
|
|
|
'Ts' => ['Ц'], |
306
|
|
|
'Ya' => ['Я'], |
307
|
|
|
'Yu' => ['Ю'], |
308
|
|
|
'Zh' => ['Ж'], |
309
|
|
|
' ' => ["\xC2\xA0", "\xE2\x80\x80", "\xE2\x80\x81", "\xE2\x80\x82", "\xE2\x80\x83", "\xE2\x80\x84", "\xE2\x80\x85", "\xE2\x80\x86", "\xE2\x80\x87", "\xE2\x80\x88", "\xE2\x80\x89", "\xE2\x80\x8A", "\xE2\x80\xAF", "\xE2\x81\x9F", "\xE3\x80\x80", "\xEF\xBE\xA0"], |
310
|
|
|
]; |
311
|
|
|
|
312
|
|
|
foreach ($characters as $key => $value) { |
313
|
|
|
$string = str_replace($value, $key, $string); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
$string = preg_replace('/[^\x20-\x7E]/u', '', $string); |
317
|
|
|
|
318
|
|
|
return $string; |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|