1 <?php
2
3 4 5 6 7
8 class rex_string
9 {
10 11 12 13 14 15 16
17 public static function size($string)
18 {
19 return mb_strlen($string, '8bit');
20 }
21
22 23 24 25 26 27 28 29 30 31
32 public static function normalizeEncoding($string)
33 {
34 static $normalizer;
35
36 if (null === $normalizer) {
37 if (function_exists('normalizer_normalize')) {
38 $normalizer = function ($string) {
39 return normalizer_normalize($string, Normalizer::FORM_C);
40 };
41 } else {
42 $normalizer = function ($string) {
43 return str_replace(
44 ["A\xcc\x88", "a\xcc\x88", "O\xcc\x88", "o\xcc\x88", "U\xcc\x88", "u\xcc\x88"],
45 ['Ä', 'ä', 'Ö', 'ö', 'Ü', 'ü'],
46 $string
47 );
48 };
49 }
50 }
51
52 return $normalizer($string);
53 }
54
55 56 57 58 59 60 61 62 63 64 65 66
67 public static function normalize($string, $replaceChar = '_', $allowedChars = '')
68 {
69 $string = self::normalizeEncoding($string);
70 $string = mb_strtolower($string);
71 $string = str_replace(['ä', 'ö', 'ü', 'ß'], ['ae', 'oe', 'ue', 'ss'], $string);
72 $string = preg_replace('/[^a-z\d' . preg_quote($allowedChars, '/') . ']+/ui', $replaceChar, $string);
73 return trim($string, $replaceChar);
74 }
75
76 77 78 79 80 81 82 83 84 85 86 87
88 public static function split($string)
89 {
90 $string = trim($string);
91 if (empty($string)) {
92 return [];
93 }
94 $result = [];
95 $spacer = '@@@REX_SPACER@@@';
96 $quoted = [];
97
98 $pattern = '@(?<=\s|=|^)(["\'])((?:.*[^\\\\])?(?:\\\\\\\\)*)\\1(?=\s|$)@Us';
99 $callback = function ($match) use ($spacer, &$quoted) {
100 $quoted[] = str_replace(['\\' . $match[1], '\\\\'], [$match[1], '\\'], $match[2]);
101 return $spacer;
102 };
103 $string = preg_replace_callback($pattern, $callback, $string);
104
105 $parts = preg_split('@\s+@', $string);
106 $i = 0;
107 foreach ($parts as $part) {
108 $part = explode('=', $part, 2);
109 if (isset($part[1])) {
110 $value = $part[1] == $spacer ? $quoted[$i++] : $part[1];
111 $result[$part[0]] = $value;
112 } else {
113 $value = $part[0] == $spacer ? $quoted[$i++] : $part[0];
114 $result[] = $value;
115 }
116 }
117
118 return $result;
119 }
120
121 122 123 124 125 126 127
128 public static function versionSplit($version)
129 {
130 return preg_split('/(?<=\d)(?=[a-z])|(?<=[a-z])(?=\d)|[ ._-]+/i', $version);
131 }
132
133 134 135 136 137 138 139 140 141 142 143 144 145 146
147 public static function versionCompare($version1, $version2, $comparator = null)
148 {
149 $version1 = self::versionSplit($version1);
150 $version2 = self::versionSplit($version2);
151 $max = max(count($version1), count($version2));
152 $version1 = implode('.', array_pad($version1, $max, '0'));
153 $version2 = implode('.', array_pad($version2, $max, '0'));
154 return version_compare($version1, $version2, $comparator);
155 }
156
157 158 159 160 161 162 163 164
165 public static function yamlEncode(array $value, $inline = 3)
166 {
167 return Symfony\Component\Yaml\Yaml::dump($value, $inline, 4);
168 }
169
170 171 172 173 174 175 176 177 178
179 public static function yamlDecode($value)
180 {
181 try {
182 return Symfony\Component\Yaml\Yaml::parse($value);
183 } catch (Symfony\Component\Yaml\Exception\ParseException $exception) {
184 throw new rex_yaml_parse_exception($exception->getMessage(), $exception);
185 }
186 }
187
188 189 190 191 192 193 194 195
196 public static function buildQuery(array $params, $argSeparator = '&')
197 {
198 $query = [];
199 $func = function (array $params, $fullkey = null) use (&$query, &$func) {
200 foreach ($params as $key => $value) {
201 $key = $fullkey ? $fullkey . '[' . urlencode($key) . ']' : urlencode($key);
202 if (is_array($value)) {
203 $func($value, $key);
204 } else {
205 $query[] = $key . '=' . str_replace('%2F', '/', urlencode($value));
206 }
207 }
208 };
209 $func($params);
210 return implode($argSeparator, $query);
211 }
212
213 214 215 216 217 218 219
220 public static function buildAttributes(array $attributes)
221 {
222 $attr = '';
223
224 foreach ($attributes as $key => $value) {
225 if (is_int($key)) {
226 $attr .= ' ' . rex_escape($value);
227 } else {
228 if (is_array($value)) {
229 $value = implode(' ', $value);
230 }
231
232 $value = str_replace('&', '&', $value);
233 $attr .= ' ' . rex_escape($key) . '="' . rex_escape($value) . '"';
234 }
235 }
236
237 return $attr;
238 }
239
240 241 242 243 244 245 246
247 public static function highlight($string)
248 {
249 $return = str_replace(["\r", "\n"], ['', ''], highlight_string($string, true));
250 return '<pre class="rex-code">' . $return . '</pre>';
251 }
252 }
253