1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette;
13:
14: use Nette;
15:
16:
17:
18: 19: 20: 21: 22:
23: class String
24: {
25:
26: 27: 28:
29: final public function __construct()
30: {
31: throw new \LogicException("Cannot instantiate static class " . get_class($this));
32: }
33:
34:
35:
36: 37: 38: 39: 40: 41:
42: public static function checkEncoding($s, $encoding = 'UTF-8')
43: {
44: return $s === self::fixEncoding($s, $encoding);
45: }
46:
47:
48:
49: 50: 51: 52: 53: 54:
55: public static function fixEncoding($s, $encoding = 'UTF-8')
56: {
57: 58: return @iconv('UTF-16', $encoding . '//IGNORE', iconv($encoding, 'UTF-16//IGNORE', $s)); 59: }
60:
61:
62:
63: 64: 65: 66: 67: 68:
69: public static function chr($code, $encoding = 'UTF-8')
70: {
71: return iconv('UTF-32BE', $encoding . '//IGNORE', pack('N', $code));
72: }
73:
74:
75:
76: 77: 78: 79: 80: 81:
82: public static function startsWith($haystack, $needle)
83: {
84: return strncmp($haystack, $needle, strlen($needle)) === 0;
85: }
86:
87:
88:
89: 90: 91: 92: 93: 94:
95: public static function endsWith($haystack, $needle)
96: {
97: return strlen($needle) === 0 || substr($haystack, -strlen($needle)) === $needle;
98: }
99:
100:
101:
102: 103: 104: 105: 106:
107: public static function normalize($s)
108: {
109: 110: $s = str_replace("\r\n", "\n", $s); 111: $s = strtr($s, "\r", "\n"); 112:
113: 114: $s = preg_replace('#[\x00-\x08\x0B-\x1F]+#', '', $s);
115:
116: 117: $s = preg_replace("#[\t ]+$#m", '', $s);
118:
119: 120: $s = trim($s, "\n");
121:
122: return $s;
123: }
124:
125:
126:
127: 128: 129: 130: 131:
132: public static function toAscii($s)
133: {
134: $s = preg_replace('#[^\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}]#u', '', $s);
135: $s = strtr($s, '`\'"^~', "\x01\x02\x03\x04\x05");
136: if (ICONV_IMPL === 'glibc') {
137: $s = @iconv('UTF-8', 'WINDOWS-1250//TRANSLIT', $s); 138: $s = strtr($s, "\xa5\xa3\xbc\x8c\xa7\x8a\xaa\x8d\x8f\x8e\xaf\xb9\xb3\xbe\x9c\x9a\xba\x9d\x9f\x9e"
139: . "\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3"
140: . "\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8"
141: . "\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xfe",
142: "ALLSSSSTZZZallssstzzzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTsraaaalccceeeeiiddnnooooruuuuyt");
143: } else {
144: $s = @iconv('UTF-8', 'ASCII//TRANSLIT', $s); 145: }
146: $s = str_replace(array('`', "'", '"', '^', '~'), '', $s);
147: return strtr($s, "\x01\x02\x03\x04\x05", '`\'"^~');
148: }
149:
150:
151:
152: 153: 154: 155: 156: 157: 158:
159: public static function webalize($s, $charlist = NULL, $lower = TRUE)
160: {
161: $s = self::toAscii($s);
162: if ($lower) $s = strtolower($s);
163: $s = preg_replace('#[^a-z0-9' . preg_quote($charlist, '#') . ']+#i', '-', $s);
164: $s = trim($s, '-');
165: return $s;
166: }
167:
168:
169:
170: 171: 172: 173: 174: 175: 176:
177: public static function truncate($s, $maxLen, $append = "\xE2\x80\xA6")
178: {
179: if (self::length($s) > $maxLen) {
180: $maxLen = $maxLen - self::length($append);
181: if ($maxLen < 1) {
182: return $append;
183:
184: } elseif ($matches = self::match($s, '#^.{1,'.$maxLen.'}(?=[\s\x00-/:-@\[-`{-~])#us')) {
185: return $matches[0] . $append;
186:
187: } else {
188: return iconv_substr($s, 0, $maxLen, 'UTF-8') . $append;
189: }
190: }
191: return $s;
192: }
193:
194:
195:
196: 197: 198: 199: 200: 201: 202:
203: public static function indent($s, $level = 1, $chars = "\t")
204: {
205: return $level < 1 ? $s : self::replace($s, '#(?:^|[\r\n]+)(?=[^\r\n])#', '$0' . str_repeat($chars, $level));
206: }
207:
208:
209:
210: 211: 212: 213: 214:
215: public static function lower($s)
216: {
217: return mb_strtolower($s, 'UTF-8');
218: }
219:
220:
221:
222: 223: 224: 225: 226:
227: public static function upper($s)
228: {
229: return mb_strtoupper($s, 'UTF-8');
230: }
231:
232:
233:
234: 235: 236: 237: 238:
239: public static function firstUpper($s)
240: {
241: return self::upper(mb_substr($s, 0, 1, 'UTF-8')) . mb_substr($s, 1, self::length($s), 'UTF-8');
242: }
243:
244:
245:
246: 247: 248: 249: 250:
251: public static function capitalize($s)
252: {
253: return mb_convert_case($s, MB_CASE_TITLE, 'UTF-8');
254: }
255:
256:
257:
258: 259: 260: 261: 262: 263: 264:
265: public static function compare($left, $right, $len = NULL)
266: {
267: if ($len < 0) {
268: $left = iconv_substr($left, $len, -$len, 'UTF-8');
269: $right = iconv_substr($right, $len, -$len, 'UTF-8');
270: } elseif ($len !== NULL) {
271: $left = iconv_substr($left, 0, $len, 'UTF-8');
272: $right = iconv_substr($right, 0, $len, 'UTF-8');
273: }
274: return self::lower($left) === self::lower($right);
275: }
276:
277:
278:
279: 280: 281: 282: 283:
284: public static function length($s)
285: {
286: return function_exists('mb_strlen') ? mb_strlen($s, 'UTF-8') : strlen(utf8_decode($s));
287: }
288:
289:
290:
291: 292: 293: 294: 295: 296:
297: public static function trim($s, $charlist = " \t\n\r\0\x0B\xC2\xA0")
298: {
299: $charlist = preg_quote($charlist, '#');
300: return self::replace($s, '#^['.$charlist.']+|['.$charlist.']+$#u', '');
301: }
302:
303:
304:
305: 306: 307: 308: 309: 310: 311:
312: public static function padLeft($s, $length, $pad = ' ')
313: {
314: $length = max(0, $length - self::length($s));
315: $padLen = self::length($pad);
316: return str_repeat($pad, $length / $padLen) . iconv_substr($pad, 0, $length % $padLen, 'UTF-8') . $s;
317: }
318:
319:
320:
321: 322: 323: 324: 325: 326: 327:
328: public static function padRight($s, $length, $pad = ' ')
329: {
330: $length = max(0, $length - self::length($s));
331: $padLen = self::length($pad);
332: return $s . str_repeat($pad, $length / $padLen) . iconv_substr($pad, 0, $length % $padLen, 'UTF-8');
333: }
334:
335:
336:
337: 338: 339: 340: 341: 342:
343: public static function random($length = 10, $charlist = '0-9a-z')
344: {
345: $charlist = str_shuffle(preg_replace_callback('#.-.#', function($m) {
346: return implode('', range($m[0][0], $m[0][2]));
347: }, $charlist));
348: $chLen = strlen($charlist);
349:
350: $s = '';
351: for ($i = 0; $i < $length; $i++) {
352: if ($i % 5 === 0) {
353: $rand = lcg_value();
354: $rand2 = microtime(TRUE);
355: }
356: $rand *= $chLen;
357: $s .= $charlist[($rand + $rand2) % $chLen];
358: $rand -= (int) $rand;
359: }
360: return $s;
361: }
362:
363:
364:
365: 366: 367: 368: 369: 370: 371:
372: public static function split($subject, $pattern, $flags = 0)
373: {
374: Debug::tryError();
375: $res = preg_split($pattern, $subject, -1, $flags | PREG_SPLIT_DELIM_CAPTURE);
376: self::catchPregError($pattern);
377: return $res;
378: }
379:
380:
381:
382: 383: 384: 385: 386: 387: 388: 389:
390: public static function match($subject, $pattern, $flags = 0, $offset = 0)
391: {
392: Debug::tryError();
393: $res = preg_match($pattern, $subject, $m, $flags, $offset);
394: self::catchPregError($pattern);
395: if ($res) {
396: return $m;
397: }
398: }
399:
400:
401:
402: 403: 404: 405: 406: 407: 408: 409:
410: public static function matchAll($subject, $pattern, $flags = 0, $offset = 0)
411: {
412: Debug::tryError();
413: $res = preg_match_all(
414: $pattern, $subject, $m,
415: ($flags & PREG_PATTERN_ORDER) ? $flags : ($flags | PREG_SET_ORDER),
416: $offset
417: );
418: self::catchPregError($pattern);
419: return $m;
420: }
421:
422:
423:
424: 425: 426: 427: 428: 429: 430: 431:
432: public static function replace($subject, $pattern, $replacement = NULL, $limit = -1)
433: {
434: Debug::tryError();
435: if (is_object($replacement) || is_array($replacement)) {
436: if ($replacement instanceof Callback) {
437: $replacement = $replacement->getNative();
438: }
439: if (!is_callable($replacement, FALSE, $textual)) {
440: Debug::catchError($foo);
441: throw new \InvalidStateException("Callback '$textual' is not callable.");
442: }
443: $res = preg_replace_callback($pattern, $replacement, $subject, $limit);
444:
445: if (Debug::catchError($e)) { 446: $trace = $e->getTrace();
447: if (isset($trace[2]['class']) && $trace[2]['class'] === __CLASS__) {
448: throw new RegexpException($e->getMessage() . " in pattern: $pattern");
449: }
450: }
451:
452: } elseif (is_array($pattern)) {
453: $res = preg_replace(array_keys($pattern), array_values($pattern), $subject, $limit);
454:
455: } else {
456: $res = preg_replace($pattern, $replacement, $subject, $limit);
457: }
458: self::catchPregError($pattern);
459: return $res;
460: }
461:
462:
463:
464:
465: public static function catchPregError($pattern)
466: {
467: if (Debug::catchError($e)) { 468: throw new RegexpException($e->getMessage() . " in pattern: $pattern");
469:
470: } elseif (preg_last_error()) { 471: static $messages = array(
472: PREG_INTERNAL_ERROR => 'Internal error',
473: PREG_BACKTRACK_LIMIT_ERROR => 'Backtrack limit was exhausted',
474: PREG_RECURSION_LIMIT_ERROR => 'Recursion limit was exhausted',
475: PREG_BAD_UTF8_ERROR => 'Malformed UTF-8 data',
476: 5 => 'Offset didn\'t correspond to the begin of a valid UTF-8 code point', 477: );
478: $code = preg_last_error();
479: throw new RegexpException((isset($messages[$code]) ? $messages[$code] : 'Unknown error') . " (pattern: $pattern)", $code);
480: }
481: }
482:
483: }
484:
485:
486:
487: 488: 489:
490: class RegexpException extends \Exception
491: {
492: }
493: