1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Matecat\Finder\Helper; |
4
|
|
|
|
5
|
|
|
class Strings |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @param string $string |
9
|
|
|
* |
10
|
|
|
* @return string |
11
|
|
|
*/ |
12
|
|
|
public static function cutNbsps($string) |
13
|
|
|
{ |
14
|
|
|
return str_replace([' ', ' '], ' ', $string); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param string $string |
19
|
|
|
* |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
|
|
public static function htmlEntityDecode($string) |
23
|
|
|
{ |
24
|
|
|
return html_entity_decode($string, ENT_QUOTES|ENT_XHTML, 'UTF-8'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $string |
29
|
|
|
* |
30
|
|
|
* @return array|bool|string[]|null |
31
|
|
|
*/ |
32
|
|
|
public static function split($string) |
33
|
|
|
{ |
34
|
|
|
return mb_str_split($string); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param int $length |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public static function token($length = 8) |
43
|
|
|
{ |
44
|
|
|
$key = ''; |
45
|
|
|
$keys = array_merge(range(0, 9), range('a', 'z')); |
46
|
|
|
|
47
|
|
|
for ($i = 0; $i < $length; $i++) { |
48
|
|
|
$key .= $keys[array_rand($keys)]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $key; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $string |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public static function isMultibyte($string) |
60
|
|
|
{ |
61
|
|
|
return ((strlen($string) - mb_strlen($string)) > 0); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $needle |
66
|
|
|
* @param $haystack |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public static function contains($needle, $haystack) |
71
|
|
|
{ |
72
|
|
|
if(empty($haystack)){ |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return strpos($haystack, $needle) !== false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public static function firstChar($string) |
80
|
|
|
{ |
81
|
|
|
return $string[0]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public static function lastChar($string) |
85
|
|
|
{ |
86
|
|
|
return $string[strlen($string) - 1]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $string |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public static function protectHTMLTags($string) |
94
|
|
|
{ |
95
|
|
|
preg_match_all('/<(.*?)>|<(.*?)>/sm', $string, $matches); |
96
|
|
|
|
97
|
|
|
if(!empty($matches[0])){ |
98
|
|
|
foreach ($matches[0] as $index => $element){ |
99
|
|
|
|
100
|
|
|
$tag = explode(" ", $element); |
101
|
|
|
$tag = str_replace(["<", ">", "<", ">", "/"], "", $tag[0]); |
102
|
|
|
|
103
|
|
|
$nextElement = $matches[0][$index+1] ?? null; |
104
|
|
|
|
105
|
|
|
if(!self::contains("/", $element)){ |
106
|
|
|
|
107
|
|
|
if($nextElement === null){ |
108
|
|
|
continue; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$nextTag = explode(" ", $nextElement); |
112
|
|
|
$nextTag = str_replace(["<", ">", "<", ">", "/"], "", $nextTag[0]); |
113
|
|
|
|
114
|
|
|
if($nextTag !== $tag){ |
115
|
|
|
continue; |
116
|
|
|
} |
117
|
|
|
} else { |
118
|
|
|
// self closing tag |
119
|
|
|
$closingTag = explode(" ", $element); |
120
|
|
|
$closingTag = str_replace(["<", ">", "<", ">"], "", $closingTag[0]); |
121
|
|
|
|
122
|
|
|
if(empty($closingTag)){ |
123
|
|
|
continue; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if(!(self::firstChar($closingTag) === "/" or self::lastChar($closingTag) === "/")){ |
127
|
|
|
continue; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$charMap = self::charMap(); |
132
|
|
|
$protectedTag = str_replace(["<", ">", "<", ">"], [$charMap["<"], $charMap[">"], $charMap["<"], $charMap[">"]], $element); |
133
|
|
|
$string = str_replace($element, $protectedTag, $string); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $string; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $string |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public static function unprotectHTMLTags($string) |
145
|
|
|
{ |
146
|
|
|
$charMap = self::charMap(); |
147
|
|
|
$string = str_replace([$charMap["<"], $charMap[">"], $charMap["<"], $charMap[">"]], ["<", ">", "<", ">"], $string); |
148
|
|
|
|
149
|
|
|
return $string; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
private static function charMap() |
156
|
|
|
{ |
157
|
|
|
return [ |
158
|
|
|
"<" => "ʃʃʃʃ", |
159
|
|
|
">" => "¶¶¶¶", |
160
|
|
|
"<" => "ɑɑɑɑ", |
161
|
|
|
">" => "ʒʒʒʒ", |
162
|
|
|
]; |
163
|
|
|
} |
164
|
|
|
} |