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