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