Namespaces

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • ArrayTools
  • Callback
  • Component
  • ComponentContainer
  • Configurator
  • DateTime
  • Debug
  • Environment
  • Framework
  • FreezableObject
  • GenericRecursiveIterator
  • Image
  • ImageMagick
  • InstanceFilterIterator
  • Object
  • ObjectMixin
  • Paginator
  • RecursiveComponentIterator
  • ServiceLocator
  • SmartCachingIterator
  • String
  • Tools

Interfaces

  • IComponent
  • IComponentContainer
  • IDebuggable
  • IServiceLocator
  • ITranslator

Exceptions

  • AmbiguousServiceException
  • Overview
  • Namespace
  • Class
  • Tree
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://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;
 13: 
 14: use Nette;
 15: 
 16: 
 17: 
 18: /**
 19:  * String tools library.
 20:  *
 21:  * @author     David Grudl
 22:  */
 23: final class String
 24: {
 25: 
 26:     /**
 27:      * Static class - cannot be instantiated.
 28:      */
 29:     final public function __construct()
 30:     {
 31:         throw new \LogicException("Cannot instantiate static class " . get_class($this));
 32:     }
 33: 
 34: 
 35: 
 36:     /**
 37:      * Checks if the string is valid for the specified encoding.
 38:      * @param  string  byte stream to check
 39:      * @param  string  expected encoding
 40:      * @return bool
 41:      */
 42:     public static function checkEncoding($s, $encoding = 'UTF-8')
 43:     {
 44:         return $s === self::fixEncoding($s, $encoding);
 45:     }
 46: 
 47: 
 48: 
 49:     /**
 50:      * Returns correctly encoded string.
 51:      * @param  string  byte stream to fix
 52:      * @param  string  encoding
 53:      * @return string
 54:      */
 55:     public static function fixEncoding($s, $encoding = 'UTF-8')
 56:     {
 57:         // removes xD800-xDFFF, xFEFF, xFFFF, x110000 and higher
 58:         $s = @iconv('UTF-16', $encoding . '//IGNORE', iconv($encoding, 'UTF-16//IGNORE', $s)); // intentionally @
 59:         return str_replace("\xEF\xBB\xBF", '', $s); // remove UTF-8 BOM
 60:     }
 61: 
 62: 
 63: 
 64:     /**
 65:      * Returns a specific character.
 66:      * @param  int     codepoint
 67:      * @param  string  encoding
 68:      * @return string
 69:      */
 70:     public static function chr($code, $encoding = 'UTF-8')
 71:     {
 72:         return iconv('UTF-32BE', $encoding . '//IGNORE', pack('N', $code));
 73:     }
 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:     /**
 91:      * Ends the $haystack string with the suffix $needle?
 92:      * @param  string
 93:      * @param  string
 94:      * @return bool
 95:      */
 96:     public static function endsWith($haystack, $needle)
 97:     {
 98:         return strlen($needle) === 0 || substr($haystack, -strlen($needle)) === $needle;
 99:     }
100: 
101: 
102: 
103:     /**
104:      * Removes special controls characters and normalizes line endings and spaces.
105:      * @param  string  UTF-8 encoding or 8-bit
106:      * @return string
107:      */
108:     public static function normalize($s)
109:     {
110:         // standardize line endings to unix-like
111:         $s = str_replace("\r\n", "\n", $s); // DOS
112:         $s = strtr($s, "\r", "\n"); // Mac
113: 
114:         // remove control characters; leave \t + \n
115:         $s = preg_replace('#[\x00-\x08\x0B-\x1F]+#', '', $s);
116: 
117:         // right trim
118:         $s = preg_replace("#[\t ]+$#m", '', $s);
119: 
120:         // trailing spaces
121:         $s = trim($s, "\n");
122: 
123:         return $s;
124:     }
125: 
126: 
127: 
128:     /**
129:      * Converts to web safe characters [a-z0-9-] text.
130:      * @param  string  UTF-8 encoding
131:      * @param  string  ASCII
132:      * @param  bool
133:      * @return string
134:      */
135:     public static function webalize($s, $charlist = NULL, $lower = TRUE)
136:     {
137:         $s = strtr($s, '`\'"^~', '-----');
138:         if (ICONV_IMPL === 'glibc') {
139:             $s = @iconv('UTF-8', 'WINDOWS-1250//TRANSLIT', $s); // intentionally @
140:             $s = strtr($s, "\xa5\xa3\xbc\x8c\xa7\x8a\xaa\x8d\x8f\x8e\xaf\xb9\xb3\xbe\x9c\x9a\xba\x9d\x9f\x9e\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2"
141:                 ."\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xfe",
142:                 "ALLSSSSTZZZallssstzzzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTsraaaalccceeeeiiddnnooooruuuuyt");
143:         } else {
144:             $s = @iconv('UTF-8', 'ASCII//TRANSLIT', $s); // intentionally @
145:         }
146:         $s = str_replace(array('`', "'", '"', '^', '~'), '', $s);
147:         if ($lower) $s = strtolower($s);
148:         $s = preg_replace('#[^a-z0-9' . preg_quote($charlist, '#') . ']+#i', '-', $s);
149:         $s = trim($s, '-');
150:         return $s;
151:     }
152: 
153: 
154: 
155:     /**
156:      * Truncates string to maximal length.
157:      * @param  string  UTF-8 encoding
158:      * @param  int
159:      * @param  string  UTF-8 encoding
160:      * @return string
161:      */
162:     public static function truncate($s, $maxLen, $append = "\xE2\x80\xA6")
163:     {
164:         if (iconv_strlen($s, 'UTF-8') > $maxLen) {
165:             $maxLen = $maxLen - iconv_strlen($append, 'UTF-8');
166:             if ($maxLen < 1) {
167:                 return $append;
168: 
169:             } elseif (preg_match('#^.{1,'.$maxLen.'}(?=[\s\x00-/:-@\[-`{-~])#us', $s, $matches)) {
170:                 return $matches[0] . $append;
171: 
172:             } else {
173:                 return iconv_substr($s, 0, $maxLen, 'UTF-8') . $append;
174:             }
175:         }
176:         return $s;
177:     }
178: 
179: 
180: 
181:     /**
182:      * Indents the content from the left.
183:      * @param  string  UTF-8 encoding or 8-bit
184:      * @param  int
185:      * @param  string
186:      * @return string
187:      */
188:     public static function indent($s, $level = 1, $chars = "\t")
189:     {
190:         return $level < 1 ? $s : preg_replace('#(?:^|[\r\n]+)(?=[^\r\n])#', '$0' . str_repeat($chars, $level), $s);
191:     }
192: 
193: 
194: 
195:     /**
196:      * Convert to lower case.
197:      * @param  string  UTF-8 encoding
198:      * @return string
199:      */
200:     public static function lower($s)
201:     {
202:         return mb_strtolower($s, 'UTF-8');
203:     }
204: 
205: 
206: 
207:     /**
208:      * Convert to upper case.
209:      * @param  string  UTF-8 encoding
210:      * @return string
211:      */
212:     public static function upper($s)
213:     {
214:         return mb_strtoupper($s, 'UTF-8');
215:     }
216: 
217: 
218: 
219:     /**
220:      * Capitalize string.
221:      * @param  string  UTF-8 encoding
222:      * @return string
223:      */
224:     public static function capitalize($s)
225:     {
226:         return mb_convert_case($s, MB_CASE_TITLE, 'UTF-8');
227:     }
228: 
229: 
230: 
231:     /**
232:      * Strips whitespace.
233:      * @param  string  UTF-8 encoding
234:      * @param  string
235:      * @return string
236:      */
237:     public static function trim($s, $charlist = " \t\n\r\0\x0B\xC2\xA0")
238:     {
239:         $charlist = preg_quote($charlist, '#');
240:         return preg_replace('#^['.$charlist.']+|['.$charlist.']+$#u', '', $s);
241:     }
242: 
243: 
244: 
245:     /**
246:      * Pad a string to a certain length with another string.
247:      * @param  string  UTF-8 encoding
248:      * @param  int
249:      * @param  string
250:      * @return string
251:      */
252:     public static function padLeft($s, $length, $pad = ' ')
253:     {
254:         $length = max(0, $length - iconv_strlen($s, 'UTF-8'));
255:         $padLen = iconv_strlen($pad, 'UTF-8');
256:         return str_repeat($pad, $length / $padLen) . iconv_substr($pad, 0, $length % $padLen, 'UTF-8') . $s;
257:     }
258: 
259: 
260: 
261:     /**
262:      * Pad a string to a certain length with another string.
263:      * @param  string  UTF-8 encoding
264:      * @param  int
265:      * @param  string
266:      * @return string
267:      */
268:     public static function padRight($s, $length, $pad = ' ')
269:     {
270:         $length = max(0, $length - iconv_strlen($s, 'UTF-8'));
271:         $padLen = iconv_strlen($pad, 'UTF-8');
272:         return $s . str_repeat($pad, $length / $padLen) . iconv_substr($pad, 0, $length % $padLen, 'UTF-8');
273:     }
274: 
275: }
276: 
Nette Framework 0.9.7 API documentation generated by ApiGen 2.3.0