Completed
Push — master ( 7acd91...c1bd00 )
by Jhorman Alexander
07:52 queued 06:31
created

helpers.php ➔ str_highlight()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
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  string $string
11
	 * @return string
12
	 */
13
	function str_trimmer($string){
14
15
		return is_string($string) ? preg_replace('/\s+/', ' ', trim($string)) : $string;
16
	}
17
}
18
19
if (! function_exists('array_filter_empty')){
20
21
	/**
22
	 * Filtra el array eliminando valores 'vacios'.
23
	 *
24
	 * @param  array $array
25
	 * @return array
26
	 */
27
	function array_filter_empty($array){
28
29
		$array = array_map('str_trimmer', $array);
30
31
		$array = array_filter($array, function($value) {
32
33
		    return ($value !== null && $value !== false && $value !== '');
34
		});
35
36
		return $array;
37
	}
38
}
39
40
if (! function_exists('array_only_filler')) {
41
42
    /**
43
     * Retorna un array con las array enviadas como parámetro. Sí no existe la key añade un valor por defecto.
44
     *
45
     * @param  array $array
46
     * @param  array $keys
47
     * @param  mixed $default
48
     * @param  array
49
     */
50
    function array_only_filler($array, $keys = [], $default = NULL){
51
52
    	$data = [];
53
54
        $keys = Arr::wrap($keys);
55
56
    	foreach ($keys as $key) {
57
58
    		$data[$key] = Arr::get($array, $key, $default);
59
    	}
60
61
    	return $data;
62
    }
63
}
64
65
if (! function_exists('array_is_assoc')) {
66
67
    /**
68
     * Verifica sí el array es asociativo.
69
     *
70
     * @param  array $array
71
     * @param  bool
72
     */
73
    function array_is_assoc($array){
74
75
    	if (array() === $array) return false;
76
77
    	return array_keys($array) !== range(0, count($array) - 1);
78
    }
79
}
80
81
if (! function_exists('array_keys_replace')) {
82
83
    /**
84
     * Retorna un array cuyas keys serán reemplazada por los valores de otro array cuya keys sean iguales.
85
     *
86
     * @param  array $array
87
     * @param  array $replace
88
     * @param  array
89
     */
90
    function array_keys_replace($array, $replace){
91
92
    	if (!array_is_assoc($array) OR !array_is_assoc($replace)) {
93
94
    		return $array;
95
    	}
96
97
    	$data = [];
98
99
    	foreach ($array as $column => $value) {
100
101
    		if (Arr::has($replace, $column)) {
102
103
    			$key = Arr::get($replace, $column);
104
105
    			$data[$key] = $value;
106
    		}
107
    	}
108
109
    	return $data;
110
    }
111
}
112
113
if (! function_exists('array_range')){
114
115
    /**
116
     * Obtiene un rango de números representado por un array asociativo.
117
     *
118
     * @param  integer $from
119
     * @param  integer $to
120
     * @return array
121
     */
122
    function array_range($from, $to){
123
124
        return array_combine(range($from, $to), range($from, $to));
125
    }
126
}
127
128
129
if (! function_exists('str_highlight')) {
130
131
    /**
132
     * Resalta la palabra que haga match.
133
     *
134
     * @param  string  $string
135
     * @param  string  $word
136
     * @param  string  $class
137
     * @return string
138
     */
139
    function str_highlight($string, $word, $class = 'highlight') {
140
141
        $keyword = $word;
0 ignored issues
show
Unused Code introduced by
$keyword is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
142
143
        $string = str_sanitize($string);
144
145
        $word = str_sanitize($word);
146
147
        if(!$word){ return $string; }
148
149
        $replace = "<span class='{$class}'>$1</span>";
150
151
        $pattern = preg_quote($word);
152
153
        return preg_replace("/($pattern)/i", $replace, $string);
154
    }
155
}
156
157
if (! function_exists('str_sanitize')) {
158
159
    /**
160
     * Remueve caracteres especiales de un string.
161
     *
162
     * @link   https://www.experts-exchange.com/questions/23363984/Regular-expression-with-accented-characters.html
163
     * @param  string $string
164
     * @return string
165
     */
166
    function str_sanitize($string) {
167
168
        $characters = [
169
            '0'    => ['°', '₀', '۰', '0'],
170
            '1'    => ['¹', '₁', '۱', '1'],
171
            '2'    => ['²', '₂', '۲', '2'],
172
            '3'    => ['³', '₃', '۳', '3'],
173
            '4'    => ['⁴', '₄', '۴', '٤', '4'],
174
            '5'    => ['⁵', '₅', '۵', '٥', '5'],
175
            '6'    => ['⁶', '₆', '۶', '٦', '6'],
176
            '7'    => ['⁷', '₇', '۷', '7'],
177
            '8'    => ['⁸', '₈', '۸', '8'],
178
            '9'    => ['⁹', '₉', '۹', '9'],
179
            'a'    => ['à', 'á', 'ả', 'ã', 'ạ', 'ă', 'ắ', 'ằ', 'ẳ', 'ẵ', 'ặ', 'â', 'ấ', 'ầ', 'ẩ', 'ẫ', 'ậ', 'ā', 'ą', 'å', 'α', 'ά', 'ἀ', 'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ', 'ἇ', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ', 'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ', 'ά', 'ᾰ', 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ', 'ᾶ', 'ᾷ', 'а', 'أ', 'အ', 'ာ', 'ါ', 'ǻ', 'ǎ', 'ª', 'ა', 'अ', 'ا', 'a', 'ä'],
180
            'b'    => ['б', 'β', 'ب', 'ဗ', 'ბ', 'b'],
181
            'c'    => ['ç', 'ć', 'č', 'ĉ', 'ċ', 'c'],
182
            'd'    => ['ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ', 'д', 'δ', 'د', 'ض', 'ဍ', 'ဒ', 'დ', 'd'],
183
            'e'    => ['é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ế', 'ề', 'ể', 'ễ', 'ệ', 'ë', 'ē', 'ę', 'ě', 'ĕ', 'ė', 'ε', 'έ', 'ἐ', 'ἑ', 'ἒ', 'ἓ', 'ἔ', 'ἕ', 'ὲ', 'έ', 'е', 'ё', 'э', 'є', 'ə', 'ဧ', 'ေ', 'ဲ', 'ე', 'ए', 'إ', 'ئ', 'e'],
184
            'f'    => ['ф', 'φ', 'ف', 'ƒ', 'ფ', 'f'],
185
            'g'    => ['ĝ', 'ğ', 'ġ', 'ģ', 'г', 'ґ', 'γ', 'ဂ', 'გ', 'گ', 'g'],
186
            'h'    => ['ĥ', 'ħ', 'η', 'ή', 'ح', 'ه', 'ဟ', 'ှ', 'ჰ', 'h'],
187
            'i'    => ['í', 'ì', 'ỉ', 'ĩ', 'ị', 'î', 'ï', 'ī', 'ĭ', 'į', 'ı', 'ι', 'ί', 'ϊ', 'ΐ', 'ἰ', 'ἱ', 'ἲ', 'ἳ', 'ἴ', 'ἵ', 'ἶ', 'ἷ', 'ὶ', 'ί', 'ῐ', 'ῑ', 'ῒ', 'ΐ', 'ῖ', 'ῗ', 'і', 'ї', 'и', 'ဣ', 'ိ', 'ီ', 'ည်', 'ǐ', 'ი', 'इ', 'ی', 'i'],
188
            'j'    => ['ĵ', 'ј', 'Ј', 'ჯ', 'ج', 'j'],
189
            'k'    => ['ķ', 'ĸ', 'к', 'κ', 'Ķ', 'ق', 'ك', 'က', 'კ', 'ქ', 'ک', 'k'],
190
            'l'    => ['ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'л', 'λ', 'ل', 'လ', 'ლ', 'l'],
191
            'm'    => ['м', 'μ', 'م', 'မ', 'მ', 'm'],
192
            'n'    => ['ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν', 'н', 'ن', 'န', 'ნ', 'n'],
193
            'o'    => ['ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ố', 'ồ', 'ổ', 'ỗ', 'ộ', 'ơ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ', 'ø', 'ō', 'ő', 'ŏ', 'ο', 'ὀ', 'ὁ', 'ὂ', 'ὃ', 'ὄ', 'ὅ', 'ὸ', 'ό', 'о', 'و', 'θ', 'ို', 'ǒ', 'ǿ', 'º', 'ო', 'ओ', 'o', 'ö'],
194
            'p'    => ['п', 'π', 'ပ', 'პ', 'پ', 'p'],
195
            'q'    => ['ყ', 'q'],
196
            'r'    => ['ŕ', 'ř', 'ŗ', 'р', 'ρ', 'ر', 'რ', 'r'],
197
            's'    => ['ś', 'š', 'ş', 'с', 'σ', 'ș', 'ς', 'س', 'ص', 'စ', 'ſ', 'ს', 's'],
198
            't'    => ['ť', 'ţ', 'т', 'τ', 'ț', 'ت', 'ط', 'ဋ', 'တ', 'ŧ', 'თ', 'ტ', 't'],
199
            'u'    => ['ú', 'ù', 'ủ', 'ũ', 'ụ', 'ư', 'ứ', 'ừ', 'ử', 'ữ', 'ự', 'û', 'ū', 'ů', 'ű', 'ŭ', 'ų', 'µ', 'у', 'ဉ', 'ု', 'ူ', 'ǔ', 'ǖ', 'ǘ', 'ǚ', 'ǜ', 'უ', 'उ', 'u', 'ў', 'ü'],
200
            'v'    => ['в', 'ვ', 'ϐ', 'v'],
201
            'w'    => ['ŵ', 'ω', 'ώ', 'ဝ', 'ွ', 'w'],
202
            'x'    => ['χ', 'ξ', 'x'],
203
            'y'    => ['ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ', 'ÿ', 'ŷ', 'й', 'ы', 'υ', 'ϋ', 'ύ', 'ΰ', 'ي', 'ယ', 'y'],
204
            'z'    => ['ź', 'ž', 'ż', 'з', 'ζ', 'ز', 'ဇ', 'ზ', 'z'],
205
            'aa'   => ['ع', 'आ', 'آ'],
206
            'ae'   => ['æ', 'ǽ'],
207
            'ai'   => ['ऐ'],
208
            'ch'   => ['ч', 'ჩ', 'ჭ', 'چ'],
209
            'dj'   => ['ђ', 'đ'],
210
            'dz'   => ['џ', 'ძ'],
211
            'ei'   => ['ऍ'],
212
            'gh'   => ['غ', 'ღ'],
213
            'ii'   => ['ई'],
214
            'ij'   => ['ij'],
215
            'kh'   => ['х', 'خ', 'ხ'],
216
            'lj'   => ['љ'],
217
            'nj'   => ['њ'],
218
            'oe'   => ['ö', 'œ', 'ؤ'],
219
            'oi'   => ['ऑ'],
220
            'oii'  => ['ऒ'],
221
            'ps'   => ['ψ'],
222
            'sh'   => ['ш', 'შ', 'ش'],
223
            'shch' => ['щ'],
224
            'ss'   => ['ß'],
225
            'sx'   => ['ŝ'],
226
            'th'   => ['þ', 'ϑ', 'ث', 'ذ', 'ظ'],
227
            'ts'   => ['ц', 'ც', 'წ'],
228
            'ue'   => ['ü'],
229
            'uu'   => ['ऊ'],
230
            'ya'   => ['я'],
231
            'yu'   => ['ю'],
232
            'zh'   => ['ж', 'ჟ', 'ژ'],
233
            '(c)'  => ['©'],
234
            'A'    => ['Á', 'À', 'Ả', 'Ã', 'Ạ', 'Ă', 'Ắ', 'Ằ', 'Ẳ', 'Ẵ', 'Ặ', 'Â', 'Ấ', 'Ầ', 'Ẩ', 'Ẫ', 'Ậ', 'Å', 'Ā', 'Ą', 'Α', 'Ά', 'Ἀ', 'Ἁ', 'Ἂ', 'Ἃ', 'Ἄ', 'Ἅ', 'Ἆ', 'Ἇ', 'ᾈ', 'ᾉ', 'ᾊ', 'ᾋ', 'ᾌ', 'ᾍ', 'ᾎ', 'ᾏ', 'Ᾰ', 'Ᾱ', 'Ὰ', 'Ά', 'ᾼ', 'А', 'Ǻ', 'Ǎ', 'A', 'Ä'],
235
            'B'    => ['Б', 'Β', 'ब', 'B'],
236
            'C'    => ['Ç', 'Ć', 'Č', 'Ĉ', 'Ċ', 'C'],
237
            'D'    => ['Ď', 'Ð', 'Đ', 'Ɖ', 'Ɗ', 'Ƌ', 'ᴅ', 'ᴆ', 'Д', 'Δ', 'D'],
238
            'E'    => ['É', 'È', 'Ẻ', 'Ẽ', 'Ẹ', 'Ê', 'Ế', 'Ề', 'Ể', 'Ễ', 'Ệ', 'Ë', 'Ē', 'Ę', 'Ě', 'Ĕ', 'Ė', 'Ε', 'Έ', 'Ἐ', 'Ἑ', 'Ἒ', 'Ἓ', 'Ἔ', 'Ἕ', 'Έ', 'Ὲ', 'Е', 'Ё', 'Э', 'Є', 'Ə', 'E'],
239
            'F'    => ['Ф', 'Φ', 'F'],
240
            'G'    => ['Ğ', 'Ġ', 'Ģ', 'Г', 'Ґ', 'Γ', 'G'],
241
            'H'    => ['Η', 'Ή', 'Ħ', 'H'],
242
            'I'    => ['Í', 'Ì', 'Ỉ', 'Ĩ', 'Ị', 'Î', 'Ï', 'Ī', 'Ĭ', 'Į', 'İ', 'Ι', 'Ί', 'Ϊ', 'Ἰ', 'Ἱ', 'Ἳ', 'Ἴ', 'Ἵ', 'Ἶ', 'Ἷ', 'Ῐ', 'Ῑ', 'Ὶ', 'Ί', 'И', 'І', 'Ї', 'Ǐ', 'ϒ', 'I'],
243
            'J'    => ['J'],
244
            'K'    => ['К', 'Κ', 'K'],
245
            'L'    => ['Ĺ', 'Ł', 'Л', 'Λ', 'Ļ', 'Ľ', 'Ŀ', 'ल', 'L'],
246
            'M'    => ['М', 'Μ', 'M'],
247
            'N'    => ['Ń', 'Ñ', 'Ň', 'Ņ', 'Ŋ', 'Н', 'Ν', 'N'],
248
            'O'    => ['Ó', 'Ò', 'Ỏ', 'Õ', 'Ọ', 'Ô', 'Ố', 'Ồ', 'Ổ', 'Ỗ', 'Ộ', 'Ơ', 'Ớ', 'Ờ', 'Ở', 'Ỡ', 'Ợ', 'Ø', 'Ō', 'Ő', 'Ŏ', 'Ο', 'Ό', 'Ὀ', 'Ὁ', 'Ὂ', 'Ὃ', 'Ὄ', 'Ὅ', 'Ὸ', 'Ό', 'О', 'Θ', 'Ө', 'Ǒ', 'Ǿ', 'O', 'Ö'],
249
            'P'    => ['П', 'Π', 'P'],
250
            'Q'    => ['Q'],
251
            'R'    => ['Ř', 'Ŕ', 'Р', 'Ρ', 'Ŗ', 'R'],
252
            'S'    => ['Ş', 'Ŝ', 'Ș', 'Š', 'Ś', 'С', 'Σ', 'S'],
253
            'T'    => ['Ť', 'Ţ', 'Ŧ', 'Ț', 'Т', 'Τ', 'T'],
254
            'U'    => ['Ú', 'Ù', 'Ủ', 'Ũ', 'Ụ', 'Ư', 'Ứ', 'Ừ', 'Ử', 'Ữ', 'Ự', 'Û', 'Ū', 'Ů', 'Ű', 'Ŭ', 'Ų', 'У', 'Ǔ', 'Ǖ', 'Ǘ', 'Ǚ', 'Ǜ', 'U', 'Ў', 'Ü'],
255
            'V'    => ['В', 'V'],
256
            'W'    => ['Ω', 'Ώ', 'Ŵ', 'W'],
257
            'X'    => ['Χ', 'Ξ', 'X'],
258
            'Y'    => ['Ý', 'Ỳ', 'Ỷ', 'Ỹ', 'Ỵ', 'Ÿ', 'Ῠ', 'Ῡ', 'Ὺ', 'Ύ', 'Ы', 'Й', 'Υ', 'Ϋ', 'Ŷ', 'Y'],
259
            'Z'    => ['Ź', 'Ž', 'Ż', 'З', 'Ζ', 'Z'],
260
            'AE'   => ['Æ', 'Ǽ'],
261
            'Ch'   => ['Ч'],
262
            'Dj'   => ['Ђ'],
263
            'Dz'   => ['Џ'],
264
            'Gx'   => ['Ĝ'],
265
            'Hx'   => ['Ĥ'],
266
            'Ij'   => ['IJ'],
267
            'Jx'   => ['Ĵ'],
268
            'Kh'   => ['Х'],
269
            'Lj'   => ['Љ'],
270
            'Nj'   => ['Њ'],
271
            'Oe'   => ['Œ'],
272
            'Ps'   => ['Ψ'],
273
            'Sh'   => ['Ш'],
274
            'Shch' => ['Щ'],
275
            'Ss'   => ['ẞ'],
276
            'Th'   => ['Þ'],
277
            'Ts'   => ['Ц'],
278
            'Ya'   => ['Я'],
279
            'Yu'   => ['Ю'],
280
            'Zh'   => ['Ж'],
281
            ' '    => ["\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"],
282
        ];
283
284
        foreach ($characters as $key => $value) {
285
286
            $string = str_replace($value, $key, $string);
287
        }
288
289
        $string = preg_replace('/[^\x20-\x7E]/u', '', $string);
290
291
        return $string;
292
    }
293
}
294