| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* This file is part of the Nette Framework (https://nette.org)
|
|
|
5 |
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
|
|
|
6 |
*/
|
|
|
7 |
|
|
|
8 |
declare(strict_types=1);
|
|
|
9 |
|
|
|
10 |
namespace Nette\Utils;
|
|
|
11 |
|
| 150 |
lars |
12 |
use JetBrains\PhpStorm\Language;
|
| 148 |
lars |
13 |
use Nette;
|
|
|
14 |
use function is_array, is_object, strlen;
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* String tools library.
|
|
|
19 |
*/
|
|
|
20 |
class Strings
|
|
|
21 |
{
|
|
|
22 |
use Nette\StaticClass;
|
|
|
23 |
|
| 399 |
lars |
24 |
public const TrimCharacters = " \t\n\r\0\x0B\u{A0}";
|
| 148 |
lars |
25 |
|
| 399 |
lars |
26 |
/** @deprecated use Strings::TrimCharacters */
|
|
|
27 |
public const TRIM_CHARACTERS = self::TrimCharacters;
|
| 148 |
lars |
28 |
|
| 399 |
lars |
29 |
|
| 148 |
lars |
30 |
/**
|
| 399 |
lars |
31 |
* @deprecated use Nette\Utils\Validator::isUnicode()
|
| 148 |
lars |
32 |
*/
|
|
|
33 |
public static function checkEncoding(string $s): bool
|
|
|
34 |
{
|
|
|
35 |
return $s === self::fixEncoding($s);
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Removes all invalid UTF-8 characters from a string.
|
|
|
41 |
*/
|
|
|
42 |
public static function fixEncoding(string $s): string
|
|
|
43 |
{
|
|
|
44 |
// removes xD800-xDFFF, x110000 and higher
|
|
|
45 |
return htmlspecialchars_decode(htmlspecialchars($s, ENT_NOQUOTES | ENT_IGNORE, 'UTF-8'), ENT_NOQUOTES);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Returns a specific character in UTF-8 from code point (number in range 0x0000..D7FF or 0xE000..10FFFF).
|
|
|
51 |
* @throws Nette\InvalidArgumentException if code point is not in valid range
|
|
|
52 |
*/
|
|
|
53 |
public static function chr(int $code): string
|
|
|
54 |
{
|
|
|
55 |
if ($code < 0 || ($code >= 0xD800 && $code <= 0xDFFF) || $code > 0x10FFFF) {
|
|
|
56 |
throw new Nette\InvalidArgumentException('Code point must be in range 0x0 to 0xD7FF or 0xE000 to 0x10FFFF.');
|
|
|
57 |
} elseif (!extension_loaded('iconv')) {
|
|
|
58 |
throw new Nette\NotSupportedException(__METHOD__ . '() requires ICONV extension that is not loaded.');
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
return iconv('UTF-32BE', 'UTF-8//IGNORE', pack('N', $code));
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
/**
|
| 399 |
lars |
66 |
* Returns a code point of specific character in UTF-8 (number in range 0x0000..D7FF or 0xE000..10FFFF).
|
| 148 |
lars |
67 |
*/
|
| 399 |
lars |
68 |
public static function ord(string $c): int
|
|
|
69 |
{
|
|
|
70 |
if (!extension_loaded('iconv')) {
|
|
|
71 |
throw new Nette\NotSupportedException(__METHOD__ . '() requires ICONV extension that is not loaded.');
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
$tmp = iconv('UTF-8', 'UTF-32BE//IGNORE', $c);
|
|
|
75 |
if (!$tmp) {
|
|
|
76 |
throw new Nette\InvalidArgumentException('Invalid UTF-8 character "' . ($c === '' ? '' : '\x' . strtoupper(bin2hex($c))) . '".');
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
return unpack('N', $tmp)[1];
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* @deprecated use str_starts_with()
|
|
|
85 |
*/
|
| 148 |
lars |
86 |
public static function startsWith(string $haystack, string $needle): bool
|
|
|
87 |
{
|
| 399 |
lars |
88 |
return str_starts_with($haystack, $needle);
|
| 148 |
lars |
89 |
}
|
|
|
90 |
|
|
|
91 |
|
|
|
92 |
/**
|
| 399 |
lars |
93 |
* @deprecated use str_ends_with()
|
| 148 |
lars |
94 |
*/
|
|
|
95 |
public static function endsWith(string $haystack, string $needle): bool
|
|
|
96 |
{
|
| 399 |
lars |
97 |
return str_ends_with($haystack, $needle);
|
| 148 |
lars |
98 |
}
|
|
|
99 |
|
|
|
100 |
|
|
|
101 |
/**
|
| 399 |
lars |
102 |
* @deprecated use str_contains()
|
| 148 |
lars |
103 |
*/
|
|
|
104 |
public static function contains(string $haystack, string $needle): bool
|
|
|
105 |
{
|
| 399 |
lars |
106 |
return str_contains($haystack, $needle);
|
| 148 |
lars |
107 |
}
|
|
|
108 |
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* Returns a part of UTF-8 string specified by starting position and length. If start is negative,
|
|
|
112 |
* the returned string will start at the start'th character from the end of string.
|
|
|
113 |
*/
|
|
|
114 |
public static function substring(string $s, int $start, ?int $length = null): string
|
|
|
115 |
{
|
|
|
116 |
if (function_exists('mb_substr')) {
|
|
|
117 |
return mb_substr($s, $start, $length, 'UTF-8'); // MB is much faster
|
|
|
118 |
} elseif (!extension_loaded('iconv')) {
|
|
|
119 |
throw new Nette\NotSupportedException(__METHOD__ . '() requires extension ICONV or MBSTRING, neither is loaded.');
|
|
|
120 |
} elseif ($length === null) {
|
|
|
121 |
$length = self::length($s);
|
|
|
122 |
} elseif ($start < 0 && $length < 0) {
|
|
|
123 |
$start += self::length($s); // unifies iconv_substr behavior with mb_substr
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
return iconv_substr($s, $start, $length, 'UTF-8');
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Removes control characters, normalizes line breaks to `\n`, removes leading and trailing blank lines,
|
|
|
132 |
* trims end spaces on lines, normalizes UTF-8 to the normal form of NFC.
|
|
|
133 |
*/
|
|
|
134 |
public static function normalize(string $s): string
|
|
|
135 |
{
|
|
|
136 |
// convert to compressed normal form (NFC)
|
|
|
137 |
if (class_exists('Normalizer', false) && ($n = \Normalizer::normalize($s, \Normalizer::FORM_C)) !== false) {
|
|
|
138 |
$s = $n;
|
|
|
139 |
}
|
|
|
140 |
|
| 399 |
lars |
141 |
$s = self::unixNewLines($s);
|
| 148 |
lars |
142 |
|
|
|
143 |
// remove control characters; leave \t + \n
|
|
|
144 |
$s = self::pcre('preg_replace', ['#[\x00-\x08\x0B-\x1F\x7F-\x9F]+#u', '', $s]);
|
|
|
145 |
|
|
|
146 |
// right trim
|
|
|
147 |
$s = self::pcre('preg_replace', ['#[\t ]+$#m', '', $s]);
|
|
|
148 |
|
|
|
149 |
// leading and trailing blank lines
|
|
|
150 |
$s = trim($s, "\n");
|
|
|
151 |
|
|
|
152 |
return $s;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
|
| 399 |
lars |
156 |
/** @deprecated use Strings::unixNewLines() */
|
|
|
157 |
public static function normalizeNewLines(string $s): string
|
|
|
158 |
{
|
|
|
159 |
return self::unixNewLines($s);
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
|
| 148 |
lars |
163 |
/**
|
| 399 |
lars |
164 |
* Converts line endings to \n used on Unix-like systems.
|
|
|
165 |
* Line endings are: \n, \r, \r\n, U+2028 line separator, U+2029 paragraph separator.
|
| 148 |
lars |
166 |
*/
|
| 399 |
lars |
167 |
public static function unixNewLines(string $s): string
|
| 148 |
lars |
168 |
{
|
| 399 |
lars |
169 |
return preg_replace("~\r\n?|\u{2028}|\u{2029}~", "\n", $s);
|
| 148 |
lars |
170 |
}
|
|
|
171 |
|
|
|
172 |
|
|
|
173 |
/**
|
| 399 |
lars |
174 |
* Converts line endings to platform-specific, i.e. \r\n on Windows and \n elsewhere.
|
|
|
175 |
* Line endings are: \n, \r, \r\n, U+2028 line separator, U+2029 paragraph separator.
|
|
|
176 |
*/
|
|
|
177 |
public static function platformNewLines(string $s): string
|
|
|
178 |
{
|
|
|
179 |
return preg_replace("~\r\n?|\n|\u{2028}|\u{2029}~", PHP_EOL, $s);
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
|
|
|
183 |
/**
|
| 148 |
lars |
184 |
* Converts UTF-8 string to ASCII, ie removes diacritics etc.
|
|
|
185 |
*/
|
|
|
186 |
public static function toAscii(string $s): string
|
|
|
187 |
{
|
|
|
188 |
$iconv = defined('ICONV_IMPL') ? trim(ICONV_IMPL, '"\'') : null;
|
|
|
189 |
static $transliterator = null;
|
|
|
190 |
if ($transliterator === null) {
|
|
|
191 |
if (class_exists('Transliterator', false)) {
|
|
|
192 |
$transliterator = \Transliterator::create('Any-Latin; Latin-ASCII');
|
|
|
193 |
} else {
|
|
|
194 |
trigger_error(__METHOD__ . "(): it is recommended to enable PHP extensions 'intl'.", E_USER_NOTICE);
|
|
|
195 |
$transliterator = false;
|
|
|
196 |
}
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
// remove control characters and check UTF-8 validity
|
|
|
200 |
$s = self::pcre('preg_replace', ['#[^\x09\x0A\x0D\x20-\x7E\xA0-\x{2FF}\x{370}-\x{10FFFF}]#u', '', $s]);
|
|
|
201 |
|
|
|
202 |
// transliteration (by Transliterator and iconv) is not optimal, replace some characters directly
|
|
|
203 |
$s = strtr($s, ["\u{201E}" => '"', "\u{201C}" => '"', "\u{201D}" => '"', "\u{201A}" => "'", "\u{2018}" => "'", "\u{2019}" => "'", "\u{B0}" => '^', "\u{42F}" => 'Ya', "\u{44F}" => 'ya', "\u{42E}" => 'Yu', "\u{44E}" => 'yu', "\u{c4}" => 'Ae', "\u{d6}" => 'Oe', "\u{dc}" => 'Ue', "\u{1e9e}" => 'Ss', "\u{e4}" => 'ae', "\u{f6}" => 'oe', "\u{fc}" => 'ue', "\u{df}" => 'ss']); // „ “ ” ‚ ‘ ’ ° Я я Ю ю Ä Ö Ü ẞ ä ö ü ß
|
|
|
204 |
if ($iconv !== 'libiconv') {
|
|
|
205 |
$s = strtr($s, ["\u{AE}" => '(R)', "\u{A9}" => '(c)', "\u{2026}" => '...', "\u{AB}" => '<<', "\u{BB}" => '>>', "\u{A3}" => 'lb', "\u{A5}" => 'yen', "\u{B2}" => '^2', "\u{B3}" => '^3', "\u{B5}" => 'u', "\u{B9}" => '^1', "\u{BA}" => 'o', "\u{BF}" => '?', "\u{2CA}" => "'", "\u{2CD}" => '_', "\u{2DD}" => '"', "\u{1FEF}" => '', "\u{20AC}" => 'EUR', "\u{2122}" => 'TM', "\u{212E}" => 'e', "\u{2190}" => '<-', "\u{2191}" => '^', "\u{2192}" => '->', "\u{2193}" => 'V', "\u{2194}" => '<->']); // ® © … « » £ Â¥ ² ³ µ ¹ º ¿ ËŠ Ë Ë á¿¯ € â„¢ â„® ↠↑ → ↓ ↔
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
if ($transliterator) {
|
|
|
209 |
$s = $transliterator->transliterate($s);
|
|
|
210 |
// use iconv because The transliterator leaves some characters out of ASCII, eg → ʾ
|
|
|
211 |
if ($iconv === 'glibc') {
|
|
|
212 |
$s = strtr($s, '?', "\x01"); // temporarily hide ? to distinguish them from the garbage that iconv creates
|
|
|
213 |
$s = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $s);
|
|
|
214 |
$s = str_replace(['?', "\x01"], ['', '?'], $s); // remove garbage and restore ? characters
|
|
|
215 |
} elseif ($iconv === 'libiconv') {
|
|
|
216 |
$s = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $s);
|
|
|
217 |
} else { // null or 'unknown' (#216)
|
|
|
218 |
$s = self::pcre('preg_replace', ['#[^\x00-\x7F]++#', '', $s]); // remove non-ascii chars
|
|
|
219 |
}
|
|
|
220 |
} elseif ($iconv === 'glibc' || $iconv === 'libiconv') {
|
|
|
221 |
// temporarily hide these characters to distinguish them from the garbage that iconv creates
|
|
|
222 |
$s = strtr($s, '`\'"^~?', "\x01\x02\x03\x04\x05\x06");
|
|
|
223 |
if ($iconv === 'glibc') {
|
|
|
224 |
// glibc implementation is very limited. transliterate into Windows-1250 and then into ASCII, so most Eastern European characters are preserved
|
|
|
225 |
$s = iconv('UTF-8', 'WINDOWS-1250//TRANSLIT//IGNORE', $s);
|
|
|
226 |
$s = strtr(
|
|
|
227 |
$s,
|
|
|
228 |
"\xa5\xa3\xbc\x8c\xa7\x8a\xaa\x8d\x8f\x8e\xaf\xb9\xb3\xbe\x9c\x9a\xba\x9d\x9f\x9e\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xfe\x96\xa0\x8b\x97\x9b\xa6\xad\xb7",
|
| 399 |
lars |
229 |
'ALLSSSSTZZZallssstzzzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTsraaaalccceeeeiiddnnooooruuuuyt- <->|-.',
|
| 148 |
lars |
230 |
);
|
|
|
231 |
$s = self::pcre('preg_replace', ['#[^\x00-\x7F]++#', '', $s]);
|
|
|
232 |
} else {
|
|
|
233 |
$s = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $s);
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
// remove garbage that iconv creates during transliteration (eg Ý -> Y')
|
|
|
237 |
$s = str_replace(['`', "'", '"', '^', '~', '?'], '', $s);
|
|
|
238 |
// restore temporarily hidden characters
|
|
|
239 |
$s = strtr($s, "\x01\x02\x03\x04\x05\x06", '`\'"^~?');
|
|
|
240 |
} else {
|
|
|
241 |
$s = self::pcre('preg_replace', ['#[^\x00-\x7F]++#', '', $s]); // remove non-ascii chars
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
return $s;
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
|
|
|
248 |
/**
|
|
|
249 |
* Modifies the UTF-8 string to the form used in the URL, ie removes diacritics and replaces all characters
|
|
|
250 |
* except letters of the English alphabet and numbers with a hyphens.
|
|
|
251 |
*/
|
|
|
252 |
public static function webalize(string $s, ?string $charlist = null, bool $lower = true): string
|
|
|
253 |
{
|
|
|
254 |
$s = self::toAscii($s);
|
|
|
255 |
if ($lower) {
|
|
|
256 |
$s = strtolower($s);
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
$s = self::pcre('preg_replace', ['#[^a-z0-9' . ($charlist !== null ? preg_quote($charlist, '#') : '') . ']+#i', '-', $s]);
|
|
|
260 |
$s = trim($s, '-');
|
|
|
261 |
return $s;
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
|
|
|
265 |
/**
|
|
|
266 |
* Truncates a UTF-8 string to given maximal length, while trying not to split whole words. Only if the string is truncated,
|
|
|
267 |
* an ellipsis (or something else set with third argument) is appended to the string.
|
|
|
268 |
*/
|
|
|
269 |
public static function truncate(string $s, int $maxLen, string $append = "\u{2026}"): string
|
|
|
270 |
{
|
|
|
271 |
if (self::length($s) > $maxLen) {
|
|
|
272 |
$maxLen -= self::length($append);
|
|
|
273 |
if ($maxLen < 1) {
|
|
|
274 |
return $append;
|
|
|
275 |
|
|
|
276 |
} elseif ($matches = self::match($s, '#^.{1,' . $maxLen . '}(?=[\s\x00-/:-@\[-`{-~])#us')) {
|
|
|
277 |
return $matches[0] . $append;
|
|
|
278 |
|
|
|
279 |
} else {
|
|
|
280 |
return self::substring($s, 0, $maxLen) . $append;
|
|
|
281 |
}
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
return $s;
|
|
|
285 |
}
|
|
|
286 |
|
|
|
287 |
|
|
|
288 |
/**
|
|
|
289 |
* Indents a multiline text from the left. Second argument sets how many indentation chars should be used,
|
|
|
290 |
* while the indent itself is the third argument (*tab* by default).
|
|
|
291 |
*/
|
|
|
292 |
public static function indent(string $s, int $level = 1, string $chars = "\t"): string
|
|
|
293 |
{
|
|
|
294 |
if ($level > 0) {
|
|
|
295 |
$s = self::replace($s, '#(?:^|[\r\n]+)(?=[^\r\n])#', '$0' . str_repeat($chars, $level));
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
return $s;
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
|
|
|
302 |
/**
|
|
|
303 |
* Converts all characters of UTF-8 string to lower case.
|
|
|
304 |
*/
|
|
|
305 |
public static function lower(string $s): string
|
|
|
306 |
{
|
|
|
307 |
return mb_strtolower($s, 'UTF-8');
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
|
|
|
311 |
/**
|
|
|
312 |
* Converts the first character of a UTF-8 string to lower case and leaves the other characters unchanged.
|
|
|
313 |
*/
|
|
|
314 |
public static function firstLower(string $s): string
|
|
|
315 |
{
|
|
|
316 |
return self::lower(self::substring($s, 0, 1)) . self::substring($s, 1);
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
|
|
|
320 |
/**
|
|
|
321 |
* Converts all characters of a UTF-8 string to upper case.
|
|
|
322 |
*/
|
|
|
323 |
public static function upper(string $s): string
|
|
|
324 |
{
|
|
|
325 |
return mb_strtoupper($s, 'UTF-8');
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
|
|
|
329 |
/**
|
|
|
330 |
* Converts the first character of a UTF-8 string to upper case and leaves the other characters unchanged.
|
|
|
331 |
*/
|
|
|
332 |
public static function firstUpper(string $s): string
|
|
|
333 |
{
|
|
|
334 |
return self::upper(self::substring($s, 0, 1)) . self::substring($s, 1);
|
|
|
335 |
}
|
|
|
336 |
|
|
|
337 |
|
|
|
338 |
/**
|
|
|
339 |
* Converts the first character of every word of a UTF-8 string to upper case and the others to lower case.
|
|
|
340 |
*/
|
|
|
341 |
public static function capitalize(string $s): string
|
|
|
342 |
{
|
|
|
343 |
return mb_convert_case($s, MB_CASE_TITLE, 'UTF-8');
|
|
|
344 |
}
|
|
|
345 |
|
|
|
346 |
|
|
|
347 |
/**
|
|
|
348 |
* Compares two UTF-8 strings or their parts, without taking character case into account. If length is null, whole strings are compared,
|
|
|
349 |
* if it is negative, the corresponding number of characters from the end of the strings is compared,
|
|
|
350 |
* otherwise the appropriate number of characters from the beginning is compared.
|
|
|
351 |
*/
|
|
|
352 |
public static function compare(string $left, string $right, ?int $length = null): bool
|
|
|
353 |
{
|
|
|
354 |
if (class_exists('Normalizer', false)) {
|
|
|
355 |
$left = \Normalizer::normalize($left, \Normalizer::FORM_D); // form NFD is faster
|
|
|
356 |
$right = \Normalizer::normalize($right, \Normalizer::FORM_D); // form NFD is faster
|
|
|
357 |
}
|
|
|
358 |
|
|
|
359 |
if ($length < 0) {
|
|
|
360 |
$left = self::substring($left, $length, -$length);
|
|
|
361 |
$right = self::substring($right, $length, -$length);
|
|
|
362 |
} elseif ($length !== null) {
|
|
|
363 |
$left = self::substring($left, 0, $length);
|
|
|
364 |
$right = self::substring($right, 0, $length);
|
|
|
365 |
}
|
|
|
366 |
|
|
|
367 |
return self::lower($left) === self::lower($right);
|
|
|
368 |
}
|
|
|
369 |
|
|
|
370 |
|
|
|
371 |
/**
|
|
|
372 |
* Finds the common prefix of strings or returns empty string if the prefix was not found.
|
|
|
373 |
* @param string[] $strings
|
|
|
374 |
*/
|
|
|
375 |
public static function findPrefix(array $strings): string
|
|
|
376 |
{
|
|
|
377 |
$first = array_shift($strings);
|
|
|
378 |
for ($i = 0; $i < strlen($first); $i++) {
|
|
|
379 |
foreach ($strings as $s) {
|
|
|
380 |
if (!isset($s[$i]) || $first[$i] !== $s[$i]) {
|
|
|
381 |
while ($i && $first[$i - 1] >= "\x80" && $first[$i] >= "\x80" && $first[$i] < "\xC0") {
|
|
|
382 |
$i--;
|
|
|
383 |
}
|
|
|
384 |
|
|
|
385 |
return substr($first, 0, $i);
|
|
|
386 |
}
|
|
|
387 |
}
|
|
|
388 |
}
|
|
|
389 |
|
|
|
390 |
return $first;
|
|
|
391 |
}
|
|
|
392 |
|
|
|
393 |
|
|
|
394 |
/**
|
|
|
395 |
* Returns number of characters (not bytes) in UTF-8 string.
|
|
|
396 |
* That is the number of Unicode code points which may differ from the number of graphemes.
|
|
|
397 |
*/
|
|
|
398 |
public static function length(string $s): int
|
|
|
399 |
{
|
|
|
400 |
return function_exists('mb_strlen')
|
|
|
401 |
? mb_strlen($s, 'UTF-8')
|
|
|
402 |
: strlen(utf8_decode($s));
|
|
|
403 |
}
|
|
|
404 |
|
|
|
405 |
|
|
|
406 |
/**
|
|
|
407 |
* Removes all left and right side spaces (or the characters passed as second argument) from a UTF-8 encoded string.
|
|
|
408 |
*/
|
| 399 |
lars |
409 |
public static function trim(string $s, string $charlist = self::TrimCharacters): string
|
| 148 |
lars |
410 |
{
|
|
|
411 |
$charlist = preg_quote($charlist, '#');
|
|
|
412 |
return self::replace($s, '#^[' . $charlist . ']+|[' . $charlist . ']+$#Du', '');
|
|
|
413 |
}
|
|
|
414 |
|
|
|
415 |
|
|
|
416 |
/**
|
|
|
417 |
* Pads a UTF-8 string to given length by prepending the $pad string to the beginning.
|
|
|
418 |
*/
|
|
|
419 |
public static function padLeft(string $s, int $length, string $pad = ' '): string
|
|
|
420 |
{
|
|
|
421 |
$length = max(0, $length - self::length($s));
|
|
|
422 |
$padLen = self::length($pad);
|
|
|
423 |
return str_repeat($pad, (int) ($length / $padLen)) . self::substring($pad, 0, $length % $padLen) . $s;
|
|
|
424 |
}
|
|
|
425 |
|
|
|
426 |
|
|
|
427 |
/**
|
|
|
428 |
* Pads UTF-8 string to given length by appending the $pad string to the end.
|
|
|
429 |
*/
|
|
|
430 |
public static function padRight(string $s, int $length, string $pad = ' '): string
|
|
|
431 |
{
|
|
|
432 |
$length = max(0, $length - self::length($s));
|
|
|
433 |
$padLen = self::length($pad);
|
|
|
434 |
return $s . str_repeat($pad, (int) ($length / $padLen)) . self::substring($pad, 0, $length % $padLen);
|
|
|
435 |
}
|
|
|
436 |
|
|
|
437 |
|
|
|
438 |
/**
|
|
|
439 |
* Reverses UTF-8 string.
|
|
|
440 |
*/
|
|
|
441 |
public static function reverse(string $s): string
|
|
|
442 |
{
|
|
|
443 |
if (!extension_loaded('iconv')) {
|
|
|
444 |
throw new Nette\NotSupportedException(__METHOD__ . '() requires ICONV extension that is not loaded.');
|
|
|
445 |
}
|
|
|
446 |
|
|
|
447 |
return iconv('UTF-32LE', 'UTF-8', strrev(iconv('UTF-8', 'UTF-32BE', $s)));
|
|
|
448 |
}
|
|
|
449 |
|
|
|
450 |
|
|
|
451 |
/**
|
|
|
452 |
* Returns part of $haystack before $nth occurence of $needle or returns null if the needle was not found.
|
|
|
453 |
* Negative value means searching from the end.
|
|
|
454 |
*/
|
|
|
455 |
public static function before(string $haystack, string $needle, int $nth = 1): ?string
|
|
|
456 |
{
|
|
|
457 |
$pos = self::pos($haystack, $needle, $nth);
|
|
|
458 |
return $pos === null
|
|
|
459 |
? null
|
|
|
460 |
: substr($haystack, 0, $pos);
|
|
|
461 |
}
|
|
|
462 |
|
|
|
463 |
|
|
|
464 |
/**
|
|
|
465 |
* Returns part of $haystack after $nth occurence of $needle or returns null if the needle was not found.
|
|
|
466 |
* Negative value means searching from the end.
|
|
|
467 |
*/
|
|
|
468 |
public static function after(string $haystack, string $needle, int $nth = 1): ?string
|
|
|
469 |
{
|
|
|
470 |
$pos = self::pos($haystack, $needle, $nth);
|
|
|
471 |
return $pos === null
|
|
|
472 |
? null
|
|
|
473 |
: substr($haystack, $pos + strlen($needle));
|
|
|
474 |
}
|
|
|
475 |
|
|
|
476 |
|
|
|
477 |
/**
|
|
|
478 |
* Returns position in characters of $nth occurence of $needle in $haystack or null if the $needle was not found.
|
|
|
479 |
* Negative value of `$nth` means searching from the end.
|
|
|
480 |
*/
|
|
|
481 |
public static function indexOf(string $haystack, string $needle, int $nth = 1): ?int
|
|
|
482 |
{
|
|
|
483 |
$pos = self::pos($haystack, $needle, $nth);
|
|
|
484 |
return $pos === null
|
|
|
485 |
? null
|
|
|
486 |
: self::length(substr($haystack, 0, $pos));
|
|
|
487 |
}
|
|
|
488 |
|
|
|
489 |
|
|
|
490 |
/**
|
|
|
491 |
* Returns position in characters of $nth occurence of $needle in $haystack or null if the needle was not found.
|
|
|
492 |
*/
|
|
|
493 |
private static function pos(string $haystack, string $needle, int $nth = 1): ?int
|
|
|
494 |
{
|
|
|
495 |
if (!$nth) {
|
|
|
496 |
return null;
|
|
|
497 |
} elseif ($nth > 0) {
|
|
|
498 |
if ($needle === '') {
|
|
|
499 |
return 0;
|
|
|
500 |
}
|
|
|
501 |
|
|
|
502 |
$pos = 0;
|
|
|
503 |
while (($pos = strpos($haystack, $needle, $pos)) !== false && --$nth) {
|
|
|
504 |
$pos++;
|
|
|
505 |
}
|
|
|
506 |
} else {
|
|
|
507 |
$len = strlen($haystack);
|
|
|
508 |
if ($needle === '') {
|
|
|
509 |
return $len;
|
|
|
510 |
} elseif ($len === 0) {
|
|
|
511 |
return null;
|
|
|
512 |
}
|
|
|
513 |
|
|
|
514 |
$pos = $len - 1;
|
|
|
515 |
while (($pos = strrpos($haystack, $needle, $pos - $len)) !== false && ++$nth) {
|
|
|
516 |
$pos--;
|
|
|
517 |
}
|
|
|
518 |
}
|
|
|
519 |
|
|
|
520 |
return Helpers::falseToNull($pos);
|
|
|
521 |
}
|
|
|
522 |
|
|
|
523 |
|
|
|
524 |
/**
|
| 399 |
lars |
525 |
* Divides the string into arrays according to the regular expression. Expressions in parentheses will be captured and returned as well.
|
| 148 |
lars |
526 |
*/
|
| 150 |
lars |
527 |
public static function split(
|
|
|
528 |
string $subject,
|
|
|
529 |
#[Language('RegExp')]
|
|
|
530 |
string $pattern,
|
| 399 |
lars |
531 |
bool|int $captureOffset = false,
|
|
|
532 |
bool $skipEmpty = false,
|
|
|
533 |
int $limit = -1,
|
|
|
534 |
bool $utf8 = false,
|
| 150 |
lars |
535 |
): array
|
| 148 |
lars |
536 |
{
|
| 399 |
lars |
537 |
$flags = is_int($captureOffset) // back compatibility
|
|
|
538 |
? $captureOffset
|
|
|
539 |
: ($captureOffset ? PREG_SPLIT_OFFSET_CAPTURE : 0) | ($skipEmpty ? PREG_SPLIT_NO_EMPTY : 0);
|
|
|
540 |
|
|
|
541 |
$pattern .= $utf8 ? 'u' : '';
|
|
|
542 |
$m = self::pcre('preg_split', [$pattern, $subject, $limit, $flags | PREG_SPLIT_DELIM_CAPTURE]);
|
|
|
543 |
return $utf8 && $captureOffset
|
|
|
544 |
? self::bytesToChars($subject, [$m])[0]
|
|
|
545 |
: $m;
|
|
|
546 |
|
| 148 |
lars |
547 |
}
|
|
|
548 |
|
|
|
549 |
|
|
|
550 |
/**
|
| 399 |
lars |
551 |
* Searches the string for the part matching the regular expression and returns
|
|
|
552 |
* an array with the found expression and individual subexpressions, or `null`.
|
| 148 |
lars |
553 |
*/
|
| 150 |
lars |
554 |
public static function match(
|
|
|
555 |
string $subject,
|
|
|
556 |
#[Language('RegExp')]
|
|
|
557 |
string $pattern,
|
| 399 |
lars |
558 |
bool|int $captureOffset = false,
|
|
|
559 |
int $offset = 0,
|
|
|
560 |
bool $unmatchedAsNull = false,
|
|
|
561 |
bool $utf8 = false,
|
| 150 |
lars |
562 |
): ?array
|
| 148 |
lars |
563 |
{
|
| 399 |
lars |
564 |
$flags = is_int($captureOffset) // back compatibility
|
|
|
565 |
? $captureOffset
|
|
|
566 |
: ($captureOffset ? PREG_OFFSET_CAPTURE : 0) | ($unmatchedAsNull ? PREG_UNMATCHED_AS_NULL : 0);
|
|
|
567 |
|
|
|
568 |
if ($utf8) {
|
|
|
569 |
$offset = strlen(self::substring($subject, 0, $offset));
|
|
|
570 |
$pattern .= 'u';
|
|
|
571 |
}
|
|
|
572 |
|
| 148 |
lars |
573 |
if ($offset > strlen($subject)) {
|
|
|
574 |
return null;
|
| 399 |
lars |
575 |
} elseif (!self::pcre('preg_match', [$pattern, $subject, &$m, $flags, $offset])) {
|
|
|
576 |
return null;
|
|
|
577 |
} elseif ($utf8 && $captureOffset) {
|
|
|
578 |
return self::bytesToChars($subject, [$m])[0];
|
|
|
579 |
} else {
|
|
|
580 |
return $m;
|
| 148 |
lars |
581 |
}
|
|
|
582 |
}
|
|
|
583 |
|
|
|
584 |
|
|
|
585 |
/**
|
| 399 |
lars |
586 |
* Searches the string for all occurrences matching the regular expression and
|
|
|
587 |
* returns an array of arrays containing the found expression and each subexpression.
|
| 148 |
lars |
588 |
*/
|
| 150 |
lars |
589 |
public static function matchAll(
|
|
|
590 |
string $subject,
|
|
|
591 |
#[Language('RegExp')]
|
|
|
592 |
string $pattern,
|
| 399 |
lars |
593 |
bool|int $captureOffset = false,
|
|
|
594 |
int $offset = 0,
|
|
|
595 |
bool $unmatchedAsNull = false,
|
|
|
596 |
bool $patternOrder = false,
|
|
|
597 |
bool $utf8 = false,
|
| 150 |
lars |
598 |
): array
|
| 148 |
lars |
599 |
{
|
| 399 |
lars |
600 |
$flags = is_int($captureOffset) // back compatibility
|
|
|
601 |
? $captureOffset
|
|
|
602 |
: ($captureOffset ? PREG_OFFSET_CAPTURE : 0) | ($unmatchedAsNull ? PREG_UNMATCHED_AS_NULL : 0) | ($patternOrder ? PREG_PATTERN_ORDER : 0);
|
|
|
603 |
|
|
|
604 |
if ($utf8) {
|
|
|
605 |
$offset = strlen(self::substring($subject, 0, $offset));
|
|
|
606 |
$pattern .= 'u';
|
|
|
607 |
}
|
|
|
608 |
|
| 148 |
lars |
609 |
if ($offset > strlen($subject)) {
|
|
|
610 |
return [];
|
|
|
611 |
}
|
|
|
612 |
|
|
|
613 |
self::pcre('preg_match_all', [
|
|
|
614 |
$pattern, $subject, &$m,
|
|
|
615 |
($flags & PREG_PATTERN_ORDER) ? $flags : ($flags | PREG_SET_ORDER),
|
|
|
616 |
$offset,
|
|
|
617 |
]);
|
| 399 |
lars |
618 |
return $utf8 && $captureOffset
|
|
|
619 |
? self::bytesToChars($subject, $m)
|
|
|
620 |
: $m;
|
|
|
621 |
|
| 148 |
lars |
622 |
}
|
|
|
623 |
|
|
|
624 |
|
|
|
625 |
/**
|
|
|
626 |
* Replaces all occurrences matching regular expression $pattern which can be string or array in the form `pattern => replacement`.
|
|
|
627 |
*/
|
| 150 |
lars |
628 |
public static function replace(
|
|
|
629 |
string $subject,
|
|
|
630 |
#[Language('RegExp')]
|
| 399 |
lars |
631 |
string|array $pattern,
|
|
|
632 |
string|callable $replacement = '',
|
|
|
633 |
int $limit = -1,
|
|
|
634 |
bool $captureOffset = false,
|
|
|
635 |
bool $unmatchedAsNull = false,
|
|
|
636 |
bool $utf8 = false,
|
| 150 |
lars |
637 |
): string
|
| 148 |
lars |
638 |
{
|
|
|
639 |
if (is_object($replacement) || is_array($replacement)) {
|
|
|
640 |
if (!is_callable($replacement, false, $textual)) {
|
|
|
641 |
throw new Nette\InvalidStateException("Callback '$textual' is not callable.");
|
|
|
642 |
}
|
|
|
643 |
|
| 399 |
lars |
644 |
$flags = ($captureOffset ? PREG_OFFSET_CAPTURE : 0) | ($unmatchedAsNull ? PREG_UNMATCHED_AS_NULL : 0);
|
|
|
645 |
if ($utf8) {
|
|
|
646 |
$pattern .= 'u';
|
|
|
647 |
if ($captureOffset) {
|
|
|
648 |
$replacement = fn($m) => $replacement(self::bytesToChars($subject, [$m])[0]);
|
|
|
649 |
}
|
|
|
650 |
}
|
| 148 |
lars |
651 |
|
| 399 |
lars |
652 |
return self::pcre('preg_replace_callback', [$pattern, $replacement, $subject, $limit, 0, $flags]);
|
|
|
653 |
|
| 148 |
lars |
654 |
} elseif (is_array($pattern) && is_string(key($pattern))) {
|
|
|
655 |
$replacement = array_values($pattern);
|
|
|
656 |
$pattern = array_keys($pattern);
|
|
|
657 |
}
|
|
|
658 |
|
| 399 |
lars |
659 |
if ($utf8) {
|
|
|
660 |
$pattern = array_map(fn($item) => $item . 'u', (array) $pattern);
|
|
|
661 |
}
|
|
|
662 |
|
| 148 |
lars |
663 |
return self::pcre('preg_replace', [$pattern, $replacement, $subject, $limit]);
|
|
|
664 |
}
|
|
|
665 |
|
|
|
666 |
|
| 399 |
lars |
667 |
private static function bytesToChars(string $s, array $groups): array
|
|
|
668 |
{
|
|
|
669 |
$lastBytes = $lastChars = 0;
|
|
|
670 |
foreach ($groups as &$matches) {
|
|
|
671 |
foreach ($matches as &$match) {
|
|
|
672 |
if ($match[1] > $lastBytes) {
|
|
|
673 |
$lastChars += self::length(substr($s, $lastBytes, $match[1] - $lastBytes));
|
|
|
674 |
} elseif ($match[1] < $lastBytes) {
|
|
|
675 |
$lastChars -= self::length(substr($s, $match[1], $lastBytes - $match[1]));
|
|
|
676 |
}
|
|
|
677 |
|
|
|
678 |
$lastBytes = $match[1];
|
|
|
679 |
$match[1] = $lastChars;
|
|
|
680 |
}
|
|
|
681 |
}
|
|
|
682 |
|
|
|
683 |
return $groups;
|
|
|
684 |
}
|
|
|
685 |
|
|
|
686 |
|
| 148 |
lars |
687 |
/** @internal */
|
|
|
688 |
public static function pcre(string $func, array $args)
|
|
|
689 |
{
|
|
|
690 |
$res = Callback::invokeSafe($func, $args, function (string $message) use ($args): void {
|
|
|
691 |
// compile-time error, not detectable by preg_last_error
|
|
|
692 |
throw new RegexpException($message . ' in pattern: ' . implode(' or ', (array) $args[0]));
|
|
|
693 |
});
|
|
|
694 |
|
|
|
695 |
if (($code = preg_last_error()) // run-time error, but preg_last_error & return code are liars
|
|
|
696 |
&& ($res === null || !in_array($func, ['preg_filter', 'preg_replace_callback', 'preg_replace'], true))
|
|
|
697 |
) {
|
| 399 |
lars |
698 |
throw new RegexpException(preg_last_error_msg()
|
| 148 |
lars |
699 |
. ' (pattern: ' . implode(' or ', (array) $args[0]) . ')', $code);
|
|
|
700 |
}
|
|
|
701 |
|
|
|
702 |
return $res;
|
|
|
703 |
}
|
|
|
704 |
}
|