Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Diagnostics
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
      • Diagnostics
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • PhpGenerator
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
  • NetteModule
  • none

Classes

  • Arrays
  • Callback
  • FileSystem
  • Finder
  • Html
  • Json
  • LimitedScope
  • MimeTypeDetector
  • Neon
  • NeonEntity
  • Paginator
  • Strings
  • Validators

Exceptions

  • AssertionException
  • JsonException
  • NeonException
  • RegexpException
  • TokenizerException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Utils;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * String tools library.
 15:  *
 16:  * @author     David Grudl
 17:  */
 18: class Strings
 19: {
 20: 
 21:     /**
 22:      * Static class - cannot be instantiated.
 23:      */
 24:     final public function __construct()
 25:     {
 26:         throw new Nette\StaticClassException;
 27:     }
 28: 
 29: 
 30:     /**
 31:      * Checks if the string is valid for the specified encoding.
 32:      * @param  string  byte stream to check
 33:      * @param  string  expected encoding
 34:      * @return bool
 35:      */
 36:     public static function checkEncoding($s, $encoding = 'UTF-8')
 37:     {
 38:         return $s === self::fixEncoding($s, $encoding);
 39:     }
 40: 
 41: 
 42:     /**
 43:      * Returns correctly encoded string.
 44:      * @param  string  byte stream to fix
 45:      * @param  string  encoding
 46:      * @return string
 47:      */
 48:     public static function fixEncoding($s, $encoding = 'UTF-8')
 49:     {
 50:         // removes xD800-xDFFF, x110000 and higher
 51:         if (PHP_VERSION_ID < 50400 || strcasecmp($encoding, 'UTF-8')) {
 52:             return @iconv('UTF-16', $encoding . '//IGNORE', iconv($encoding, 'UTF-16//IGNORE', $s)); // intentionally @
 53:         } else {
 54:             return htmlspecialchars_decode(htmlspecialchars($s, ENT_NOQUOTES | ENT_IGNORE, 'UTF-8'), ENT_NOQUOTES);
 55:         }
 56:     }
 57: 
 58: 
 59:     /**
 60:      * Returns a specific character.
 61:      * @param  int     codepoint
 62:      * @param  string  encoding
 63:      * @return string
 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:      * Starts the $haystack string with the prefix $needle?
 73:      * @param  string
 74:      * @param  string
 75:      * @return bool
 76:      */
 77:     public static function startsWith($haystack, $needle)
 78:     {
 79:         return strncmp($haystack, $needle, strlen($needle)) === 0;
 80:     }
 81: 
 82: 
 83:     /**
 84:      * Ends the $haystack string with the suffix $needle?
 85:      * @param  string
 86:      * @param  string
 87:      * @return bool
 88:      */
 89:     public static function endsWith($haystack, $needle)
 90:     {
 91:         return strlen($needle) === 0 || substr($haystack, -strlen($needle)) === $needle;
 92:     }
 93: 
 94: 
 95:     /**
 96:      * Does $haystack contain $needle?
 97:      * @param  string
 98:      * @param  string
 99:      * @return bool
100:      */
101:     public static function contains($haystack, $needle)
102:     {
103:         return strpos($haystack, $needle) !== FALSE;
104:     }
105: 
106: 
107:     /**
108:      * Returns a part of UTF-8 string.
109:      * @param  string
110:      * @param  int
111:      * @param  int
112:      * @return string
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'); // MB is much faster
121:         }
122:         return iconv_substr($s, $start, $length, 'UTF-8');
123:     }
124: 
125: 
126:     /**
127:      * Removes special controls characters and normalizes line endings and spaces.
128:      * @param  string  UTF-8 encoding or 8-bit
129:      * @return string
130:      */
131:     public static function normalize($s)
132:     {
133:         $s = self::normalizeNewLines($s);
134: 
135:         // remove control characters; leave \t + \n
136:         $s = preg_replace('#[\x00-\x08\x0B-\x1F\x7F]+#', '', $s);
137: 
138:         // right trim
139:         $s = preg_replace('#[\t ]+$#m', '', $s);
140: 
141:         // leading and trailing blank lines
142:         $s = trim($s, "\n");
143: 
144:         return $s;
145:     }
146: 
147: 
148:     /**
149:      * Standardize line endings to unix-like.
150:      * @param  string  UTF-8 encoding or 8-bit
151:      * @return string
152:      */
153:     public static function normalizeNewLines($s)
154:     {
155:         return str_replace(array("\r\n", "\r"), "\n", $s);
156:     }
157: 
158: 
159:     /**
160:      * Converts to ASCII.
161:      * @param  string  UTF-8 encoding
162:      * @return string  ASCII
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); // intentionally @
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); // intentionally @
180:         }
181:         $s = str_replace(array('`', "'", '"', '^', '~'), '', $s);
182:         return strtr($s, "\x01\x02\x03\x04\x05", '`\'"^~');
183:     }
184: 
185: 
186:     /**
187:      * Converts to web safe characters [a-z0-9-] text.
188:      * @param  string  UTF-8 encoding
189:      * @param  string  allowed characters
190:      * @param  bool
191:      * @return string
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:      * Truncates string to maximal length.
207:      * @param  string  UTF-8 encoding
208:      * @param  int
209:      * @param  string  UTF-8 encoding
210:      * @return string
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:      * Indents the content from the left.
232:      * @param  string  UTF-8 encoding or 8-bit
233:      * @param  int
234:      * @param  string
235:      * @return string
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:      * Convert to lower case.
248:      * @param  string  UTF-8 encoding
249:      * @return string
250:      */
251:     public static function lower($s)
252:     {
253:         return mb_strtolower($s, 'UTF-8');
254:     }
255: 
256: 
257:     /**
258:      * Convert to upper case.
259:      * @param  string  UTF-8 encoding
260:      * @return string
261:      */
262:     public static function upper($s)
263:     {
264:         return mb_strtoupper($s, 'UTF-8');
265:     }
266: 
267: 
268:     /**
269:      * Convert first character to upper case.
270:      * @param  string  UTF-8 encoding
271:      * @return string
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:      * Capitalize string.
281:      * @param  string  UTF-8 encoding
282:      * @return string
283:      */
284:     public static function capitalize($s)
285:     {
286:         return mb_convert_case($s, MB_CASE_TITLE, 'UTF-8');
287:     }
288: 
289: 
290:     /**
291:      * Case-insensitive compares UTF-8 strings.
292:      * @param  string
293:      * @param  string
294:      * @param  int
295:      * @return bool
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:      * Finds the length of common prefix of strings.
312:      * @param  string|array
313:      * @return string
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:      * Returns UTF-8 string length.
337:      * @param  string
338:      * @return int
339:      */
340:     public static function length($s)
341:     {
342:         return strlen(utf8_decode($s)); // fastest way
343:     }
344: 
345: 
346:     /**
347:      * Strips whitespace.
348:      * @param  string  UTF-8 encoding
349:      * @param  string
350:      * @return string
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:      * Pad a string to a certain length with another string.
361:      * @param  string  UTF-8 encoding
362:      * @param  int
363:      * @param  string
364:      * @return string
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:      * Pad a string to a certain length with another string.
376:      * @param  string  UTF-8 encoding
377:      * @param  int
378:      * @param  string
379:      * @return string
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:      * Reverse string.
391:      * @param  string  UTF-8 encoding
392:      * @return string
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:      * Generate random string.
402:      * @param  int
403:      * @param  string
404:      * @return string
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')) // slow in PHP 5.3 & Windows
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'))) { // PHP bug #52523
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:      * Splits string by a regular expression.
445:      * @param  string
446:      * @param  string
447:      * @param  int
448:      * @return array
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:      * Performs a regular expression match.
458:      * @param  string
459:      * @param  string
460:      * @param  int  can be PREG_OFFSET_CAPTURE (returned in bytes)
461:      * @param  int  offset in bytes
462:      * @return mixed
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:      * Performs a global regular expression match.
477:      * @param  string
478:      * @param  string
479:      * @param  int  can be PREG_OFFSET_CAPTURE (returned in bytes); PREG_SET_ORDER is default
480:      * @param  int  offset in bytes
481:      * @return array
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:      * Perform a regular expression search and replace.
499:      * @param  string
500:      * @param  string|array
501:      * @param  string|callable
502:      * @param  int
503:      * @return string
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:     /** @internal */
527:     public static function pcre($func, $args)
528:     {
529:         $res = Callback::invokeSafe($func, $args, function($message) use ($args) {
530:             // compile-time error, not detectable by preg_last_error
531:             throw new RegexpException($message . ' in pattern: ' . implode(' or ', (array) $args[0]));
532:         });
533: 
534:         if (($code = preg_last_error()) // run-time error, but preg_last_error & return code are liars
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:  * The exception that indicates error of the last Regexp execution.
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', // PREG_BAD_UTF8_OFFSET_ERROR
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: 
Nette Framework 2.1.8 API API documentation generated by ApiGen 2.8.0