Packages

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