Namespaces

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

Classes

  • Arrays
  • Finder
  • Html
  • Json
  • LimitedScope
  • MimeTypeDetector
  • Neon
  • NeonEntity
  • Paginator
  • Strings
  • Tokenizer
  • 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:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  */
 11: 
 12: namespace Nette\Utils;
 13: 
 14: use Nette,
 15:     Nette\Diagnostics\Debugger;
 16: 
 17: 
 18: 
 19: /**
 20:  * String tools library.
 21:  *
 22:  * @author     David Grudl
 23:  */
 24: class Strings
 25: {
 26: 
 27:     /**
 28:      * Static class - cannot be instantiated.
 29:      */
 30:     final public function __construct()
 31:     {
 32:         throw new Nette\StaticClassException;
 33:     }
 34: 
 35: 
 36: 
 37:     /**
 38:      * Checks if the string is valid for the specified encoding.
 39:      * @param  string  byte stream to check
 40:      * @param  string  expected encoding
 41:      * @return bool
 42:      */
 43:     public static function checkEncoding($s, $encoding = 'UTF-8')
 44:     {
 45:         return $s === self::fixEncoding($s, $encoding);
 46:     }
 47: 
 48: 
 49: 
 50:     /**
 51:      * Returns correctly encoded string.
 52:      * @param  string  byte stream to fix
 53:      * @param  string  encoding
 54:      * @return string
 55:      */
 56:     public static function fixEncoding($s, $encoding = 'UTF-8')
 57:     {
 58:         // removes xD800-xDFFF, xFEFF, x110000 and higher
 59:         if (strcasecmp($encoding, 'UTF-8') === 0) {
 60:             $s = str_replace("\xEF\xBB\xBF", '', $s); // remove UTF-8 BOM
 61:         }
 62:         if (PHP_VERSION_ID >= 50400) {
 63:             ini_set('mbstring.substitute_character', 'none');
 64:             return mb_convert_encoding($s, $encoding, $encoding);
 65:         }
 66:         return @iconv('UTF-16', $encoding . '//IGNORE', iconv($encoding, 'UTF-16//IGNORE', $s)); // intentionally @
 67:     }
 68: 
 69: 
 70: 
 71:     /**
 72:      * Returns a specific character.
 73:      * @param  int     codepoint
 74:      * @param  string  encoding
 75:      * @return string
 76:      */
 77:     public static function chr($code, $encoding = 'UTF-8')
 78:     {
 79:         return iconv('UTF-32BE', $encoding . '//IGNORE', pack('N', $code));
 80:     }
 81: 
 82: 
 83: 
 84:     /**
 85:      * Starts the $haystack string with the prefix $needle?
 86:      * @param  string
 87:      * @param  string
 88:      * @return bool
 89:      */
 90:     public static function startsWith($haystack, $needle)
 91:     {
 92:         return strncmp($haystack, $needle, strlen($needle)) === 0;
 93:     }
 94: 
 95: 
 96: 
 97:     /**
 98:      * Ends the $haystack string with the suffix $needle?
 99:      * @param  string
100:      * @param  string
101:      * @return bool
102:      */
103:     public static function endsWith($haystack, $needle)
104:     {
105:         return strlen($needle) === 0 || substr($haystack, -strlen($needle)) === $needle;
106:     }
107: 
108: 
109: 
110:     /**
111:      * Does $haystack contain $needle?
112:      * @param  string
113:      * @param  string
114:      * @return bool
115:      */
116:     public static function contains($haystack, $needle)
117:     {
118:         return strpos($haystack, $needle) !== FALSE;
119:     }
120: 
121: 
122: 
123:     /**
124:      * Returns a part of UTF-8 string.
125:      * @param  string
126:      * @param  int
127:      * @param  int
128:      * @return string
129:      */
130:     public static function substring($s, $start, $length = NULL)
131:     {
132:         if ($length === NULL) {
133:             $length = self::length($s);
134:         }
135:         return function_exists('mb_substr') ? mb_substr($s, $start, $length, 'UTF-8') : iconv_substr($s, $start, $length, 'UTF-8'); // MB is much faster
136:     }
137: 
138: 
139: 
140:     /**
141:      * Removes special controls characters and normalizes line endings and spaces.
142:      * @param  string  UTF-8 encoding or 8-bit
143:      * @return string
144:      */
145:     public static function normalize($s)
146:     {
147:         // standardize line endings to unix-like
148:         $s = str_replace("\r\n", "\n", $s); // DOS
149:         $s = strtr($s, "\r", "\n"); // Mac
150: 
151:         // remove control characters; leave \t + \n
152:         $s = preg_replace('#[\x00-\x08\x0B-\x1F\x7F]+#', '', $s);
153: 
154:         // right trim
155:         $s = preg_replace('#[\t ]+$#m', '', $s);
156: 
157:         // leading and trailing blank lines
158:         $s = trim($s, "\n");
159: 
160:         return $s;
161:     }
162: 
163: 
164: 
165:     /**
166:      * Converts to ASCII.
167:      * @param  string  UTF-8 encoding
168:      * @return string  ASCII
169:      */
170:     public static function toAscii($s)
171:     {
172:         $s = preg_replace('#[^\x09\x0A\x0D\x20-\x7E\xA0-\x{2FF}\x{370}-\x{10FFFF}]#u', '', $s);
173:         $s = strtr($s, '`\'"^~', "\x01\x02\x03\x04\x05");
174:         if (ICONV_IMPL === 'glibc') {
175:             $s = @iconv('UTF-8', 'WINDOWS-1250//TRANSLIT', $s); // intentionally @
176:             $s = strtr($s, "\xa5\xa3\xbc\x8c\xa7\x8a\xaa\x8d\x8f\x8e\xaf\xb9\xb3\xbe\x9c\x9a\xba\x9d\x9f\x9e"
177:                 . "\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3"
178:                 . "\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8"
179:                 . "\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xfe",
180:                 "ALLSSSSTZZZallssstzzzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTsraaaalccceeeeiiddnnooooruuuuyt");
181:         } else {
182:             $s = @iconv('UTF-8', 'ASCII//TRANSLIT', $s); // intentionally @
183:         }
184:         $s = str_replace(array('`', "'", '"', '^', '~'), '', $s);
185:         return strtr($s, "\x01\x02\x03\x04\x05", '`\'"^~');
186:     }
187: 
188: 
189: 
190:     /**
191:      * Converts to web safe characters [a-z0-9-] text.
192:      * @param  string  UTF-8 encoding
193:      * @param  string  allowed characters
194:      * @param  bool
195:      * @return string
196:      */
197:     public static function webalize($s, $charlist = NULL, $lower = TRUE)
198:     {
199:         $s = self::toAscii($s);
200:         if ($lower) {
201:             $s = strtolower($s);
202:         }
203:         $s = preg_replace('#[^a-z0-9' . preg_quote($charlist, '#') . ']+#i', '-', $s);
204:         $s = trim($s, '-');
205:         return $s;
206:     }
207: 
208: 
209: 
210:     /**
211:      * Truncates string to maximal length.
212:      * @param  string  UTF-8 encoding
213:      * @param  int
214:      * @param  string  UTF-8 encoding
215:      * @return string
216:      */
217:     public static function truncate($s, $maxLen, $append = "\xE2\x80\xA6")
218:     {
219:         if (self::length($s) > $maxLen) {
220:             $maxLen = $maxLen - self::length($append);
221:             if ($maxLen < 1) {
222:                 return $append;
223: 
224:             } elseif ($matches = self::match($s, '#^.{1,'.$maxLen.'}(?=[\s\x00-/:-@\[-`{-~])#us')) {
225:                 return $matches[0] . $append;
226: 
227:             } else {
228:                 return self::substring($s, 0, $maxLen) . $append;
229:             }
230:         }
231:         return $s;
232:     }
233: 
234: 
235: 
236:     /**
237:      * Indents the content from the left.
238:      * @param  string  UTF-8 encoding or 8-bit
239:      * @param  int
240:      * @param  string
241:      * @return string
242:      */
243:     public static function indent($s, $level = 1, $chars = "\t")
244:     {
245:         return $level < 1 ? $s : self::replace($s, '#(?:^|[\r\n]+)(?=[^\r\n])#', '$0' . str_repeat($chars, $level));
246:     }
247: 
248: 
249: 
250:     /**
251:      * Convert to lower case.
252:      * @param  string  UTF-8 encoding
253:      * @return string
254:      */
255:     public static function lower($s)
256:     {
257:         return mb_strtolower($s, 'UTF-8');
258:     }
259: 
260: 
261: 
262:     /**
263:      * Convert to upper case.
264:      * @param  string  UTF-8 encoding
265:      * @return string
266:      */
267:     public static function upper($s)
268:     {
269:         return mb_strtoupper($s, 'UTF-8');
270:     }
271: 
272: 
273: 
274:     /**
275:      * Convert first character to upper case.
276:      * @param  string  UTF-8 encoding
277:      * @return string
278:      */
279:     public static function firstUpper($s)
280:     {
281:         return self::upper(self::substring($s, 0, 1)) . self::substring($s, 1);
282:     }
283: 
284: 
285: 
286:     /**
287:      * Capitalize string.
288:      * @param  string  UTF-8 encoding
289:      * @return string
290:      */
291:     public static function capitalize($s)
292:     {
293:         return mb_convert_case($s, MB_CASE_TITLE, 'UTF-8');
294:     }
295: 
296: 
297: 
298:     /**
299:      * Case-insensitive compares UTF-8 strings.
300:      * @param  string
301:      * @param  string
302:      * @param  int
303:      * @return bool
304:      */
305:     public static function compare($left, $right, $len = NULL)
306:     {
307:         if ($len < 0) {
308:             $left = self::substring($left, $len, -$len);
309:             $right = self::substring($right, $len, -$len);
310:         } elseif ($len !== NULL) {
311:             $left = self::substring($left, 0, $len);
312:             $right = self::substring($right, 0, $len);
313:         }
314:         return self::lower($left) === self::lower($right);
315:     }
316: 
317: 
318: 
319:     /**
320:      * Returns UTF-8 string length.
321:      * @param  string
322:      * @return int
323:      */
324:     public static function length($s)
325:     {
326:         return strlen(utf8_decode($s)); // fastest way
327:     }
328: 
329: 
330: 
331:     /**
332:      * Strips whitespace.
333:      * @param  string  UTF-8 encoding
334:      * @param  string
335:      * @return string
336:      */
337:     public static function trim($s, $charlist = " \t\n\r\0\x0B\xC2\xA0")
338:     {
339:         $charlist = preg_quote($charlist, '#');
340:         return self::replace($s, '#^['.$charlist.']+|['.$charlist.']+\z#u', '');
341:     }
342: 
343: 
344: 
345:     /**
346:      * Pad a string to a certain length with another string.
347:      * @param  string  UTF-8 encoding
348:      * @param  int
349:      * @param  string
350:      * @return string
351:      */
352:     public static function padLeft($s, $length, $pad = ' ')
353:     {
354:         $length = max(0, $length - self::length($s));
355:         $padLen = self::length($pad);
356:         return str_repeat($pad, $length / $padLen) . self::substring($pad, 0, $length % $padLen) . $s;
357:     }
358: 
359: 
360: 
361:     /**
362:      * Pad a string to a certain length with another string.
363:      * @param  string  UTF-8 encoding
364:      * @param  int
365:      * @param  string
366:      * @return string
367:      */
368:     public static function padRight($s, $length, $pad = ' ')
369:     {
370:         $length = max(0, $length - self::length($s));
371:         $padLen = self::length($pad);
372:         return $s . str_repeat($pad, $length / $padLen) . self::substring($pad, 0, $length % $padLen);
373:     }
374: 
375: 
376: 
377:     /**
378:      * Reverse string.
379:      * @param  string  UTF-8 encoding
380:      * @return string
381:      */
382:     public static function reverse($s)
383:     {
384:         return @iconv('UTF-32LE', 'UTF-8', strrev(@iconv('UTF-8', 'UTF-32BE', $s)));
385:     }
386: 
387: 
388: 
389:     /**
390:      * Generate random string.
391:      * @param  int
392:      * @param  string
393:      * @return string
394:      */
395:     public static function random($length = 10, $charlist = '0-9a-z')
396:     {
397:         $charlist = str_shuffle(preg_replace_callback('#.-.#', function($m) {
398:             return implode('', range($m[0][0], $m[0][2]));
399:         }, $charlist));
400:         $chLen = strlen($charlist);
401: 
402:         $s = '';
403:         for ($i = 0; $i < $length; $i++) {
404:             if ($i % 5 === 0) {
405:                 $rand = lcg_value();
406:                 $rand2 = microtime(TRUE);
407:             }
408:             $rand *= $chLen;
409:             $s .= $charlist[($rand + $rand2) % $chLen];
410:             $rand -= (int) $rand;
411:         }
412:         return $s;
413:     }
414: 
415: 
416: 
417:     /**
418:      * Splits string by a regular expression.
419:      * @param  string
420:      * @param  string
421:      * @param  int
422:      * @return array
423:      */
424:     public static function split($subject, $pattern, $flags = 0)
425:     {
426:         set_error_handler(function($severity, $message) use ($pattern) { // preg_last_error does not return compile errors
427:             restore_error_handler();
428:             throw new RegexpException("$message in pattern: $pattern");
429:         });
430:         $res = preg_split($pattern, $subject, -1, $flags | PREG_SPLIT_DELIM_CAPTURE);
431:         restore_error_handler();
432:         if (preg_last_error()) { // run-time error
433:             throw new RegexpException(NULL, preg_last_error(), $pattern);
434:         }
435:         return $res;
436:     }
437: 
438: 
439: 
440:     /**
441:      * Performs a regular expression match.
442:      * @param  string
443:      * @param  string
444:      * @param  int  can be PREG_OFFSET_CAPTURE (returned in bytes)
445:      * @param  int  offset in bytes
446:      * @return mixed
447:      */
448:     public static function match($subject, $pattern, $flags = 0, $offset = 0)
449:     {
450:         if ($offset > strlen($subject)) {
451:             return NULL;
452:         }
453:         set_error_handler(function($severity, $message) use ($pattern) { // preg_last_error does not return compile errors
454:             restore_error_handler();
455:             throw new RegexpException("$message in pattern: $pattern");
456:         });
457:         $res = preg_match($pattern, $subject, $m, $flags, $offset);
458:         restore_error_handler();
459:         if (preg_last_error()) { // run-time error
460:             throw new RegexpException(NULL, preg_last_error(), $pattern);
461:         }
462:         if ($res) {
463:             return $m;
464:         }
465:     }
466: 
467: 
468: 
469:     /**
470:      * Performs a global regular expression match.
471:      * @param  string
472:      * @param  string
473:      * @param  int  can be PREG_OFFSET_CAPTURE (returned in bytes); PREG_SET_ORDER is default
474:      * @param  int  offset in bytes
475:      * @return array
476:      */
477:     public static function matchAll($subject, $pattern, $flags = 0, $offset = 0)
478:     {
479:         if ($offset > strlen($subject)) {
480:             return array();
481:         }
482:         set_error_handler(function($severity, $message) use ($pattern) { // preg_last_error does not return compile errors
483:             restore_error_handler();
484:             throw new RegexpException("$message in pattern: $pattern");
485:         });
486:         $res = preg_match_all(
487:             $pattern, $subject, $m,
488:             ($flags & PREG_PATTERN_ORDER) ? $flags : ($flags | PREG_SET_ORDER),
489:             $offset
490:         );
491:         restore_error_handler();
492:         if (preg_last_error()) { // run-time error
493:             throw new RegexpException(NULL, preg_last_error(), $pattern);
494:         }
495:         return $m;
496:     }
497: 
498: 
499: 
500:     /**
501:      * Perform a regular expression search and replace.
502:      * @param  string
503:      * @param  string|array
504:      * @param  string|callable
505:      * @param  int
506:      * @return string
507:      */
508:     public static function replace($subject, $pattern, $replacement = NULL, $limit = -1)
509:     {
510:         if (is_object($replacement) || is_array($replacement)) {
511:             if ($replacement instanceof Nette\Callback) {
512:                 $replacement = $replacement->getNative();
513:             }
514:             if (!is_callable($replacement, FALSE, $textual)) {
515:                 throw new Nette\InvalidStateException("Callback '$textual' is not callable.");
516:             }
517: 
518:             set_error_handler(function($severity, $message) use (& $tmp) { // preg_last_error does not return compile errors
519:                 restore_error_handler();
520:                 throw new RegexpException("$message in pattern: $tmp");
521:             });
522:             foreach ((array) $pattern as $tmp) {
523:                 preg_match($tmp, '');
524:             }
525:             restore_error_handler();
526: 
527:             $res = preg_replace_callback($pattern, $replacement, $subject, $limit);
528:             if ($res === NULL && preg_last_error()) { // run-time error
529:                 throw new RegexpException(NULL, preg_last_error(), $pattern);
530:             }
531:             return $res;
532: 
533:         } elseif ($replacement === NULL && is_array($pattern)) {
534:             $replacement = array_values($pattern);
535:             $pattern = array_keys($pattern);
536:         }
537: 
538:         set_error_handler(function($severity, $message) use ($pattern) { // preg_last_error does not return compile errors
539:             restore_error_handler();
540:             throw new RegexpException("$message in pattern: " . implode(' or ', (array) $pattern));
541:         });
542:         $res = preg_replace($pattern, $replacement, $subject, $limit);
543:         restore_error_handler();
544:         if (preg_last_error()) { // run-time error
545:             throw new RegexpException(NULL, preg_last_error(), implode(' or ', (array) $pattern));
546:         }
547:         return $res;
548:     }
549: 
550: }
551: 
552: 
553: 
554: /**
555:  * The exception that indicates error of the last Regexp execution.
556:  */
557: class RegexpException extends \Exception
558: {
559:     static public $messages = array(
560:         PREG_INTERNAL_ERROR => 'Internal error',
561:         PREG_BACKTRACK_LIMIT_ERROR => 'Backtrack limit was exhausted',
562:         PREG_RECURSION_LIMIT_ERROR => 'Recursion limit was exhausted',
563:         PREG_BAD_UTF8_ERROR => 'Malformed UTF-8 data',
564:         5 => 'Offset didn\'t correspond to the begin of a valid UTF-8 code point', // PREG_BAD_UTF8_OFFSET_ERROR
565:     );
566: 
567:     public function __construct($message, $code = NULL, $pattern = NULL)
568:     {
569:         if (!$message) {
570:             $message = (isset(self::$messages[$code]) ? self::$messages[$code] : 'Unknown error') . ($pattern ? " (pattern: $pattern)" : '');
571:         }
572:         parent::__construct($message, $code);
573:     }
574: 
575: }
576: 
Nette Framework 2.0.7 API API documentation generated by ApiGen 2.8.0