Packages

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

Classes

  • NArrayTools
  • NCallback
  • NComponent
  • NComponentContainer
  • NConfigurator
  • NDateTime53
  • NDebug
  • NEnvironment
  • NFramework
  • NFreezableObject
  • NGenericRecursiveIterator
  • NImage
  • NImageMagick
  • NInstanceFilterIterator
  • NObject
  • NObjectMixin
  • NPaginator
  • NRecursiveComponentIterator
  • NServiceLocator
  • NSmartCachingIterator
  • NString
  • NTools

Interfaces

  • IComponent
  • IComponentContainer
  • IDebuggable
  • IServiceLocator
  • ITranslator

Exceptions

  • NAmbiguousServiceException
  • Overview
  • Package
  • 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:  * @package Nette
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * String tools library.
 17:  *
 18:  * @author     David Grudl
 19:  * @package Nette
 20:  */
 21: final class NString
 22: {
 23: 
 24:     /**
 25:      * Static class - cannot be instantiated.
 26:      */
 27:     final public function __construct()
 28:     {
 29:         throw new LogicException("Cannot instantiate static class " . get_class($this));
 30:     }
 31: 
 32: 
 33: 
 34:     /**
 35:      * Checks if the string is valid for the specified encoding.
 36:      * @param  string  byte stream to check
 37:      * @param  string  expected encoding
 38:      * @return bool
 39:      */
 40:     public static function checkEncoding($s, $encoding = 'UTF-8')
 41:     {
 42:         return $s === self::fixEncoding($s, $encoding);
 43:     }
 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, xFFFF, x110000 and higher
 56:         $s = @iconv('UTF-16', $encoding . '//IGNORE', iconv($encoding, 'UTF-16//IGNORE', $s)); // intentionally @
 57:         return str_replace("\xEF\xBB\xBF", '', $s); // remove UTF-8 BOM
 58:     }
 59: 
 60: 
 61: 
 62:     /**
 63:      * Returns a specific character.
 64:      * @param  int     codepoint
 65:      * @param  string  encoding
 66:      * @return string
 67:      */
 68:     public static function chr($code, $encoding = 'UTF-8')
 69:     {
 70:         return iconv('UTF-32BE', $encoding . '//IGNORE', pack('N', $code));
 71:     }
 72: 
 73: 
 74: 
 75:     /**
 76:      * Starts the $haystack string with the prefix $needle?
 77:      * @param  string
 78:      * @param  string
 79:      * @return bool
 80:      */
 81:     public static function startsWith($haystack, $needle)
 82:     {
 83:         return strncmp($haystack, $needle, strlen($needle)) === 0;
 84:     }
 85: 
 86: 
 87: 
 88:     /**
 89:      * Ends the $haystack string with the suffix $needle?
 90:      * @param  string
 91:      * @param  string
 92:      * @return bool
 93:      */
 94:     public static function endsWith($haystack, $needle)
 95:     {
 96:         return strlen($needle) === 0 || substr($haystack, -strlen($needle)) === $needle;
 97:     }
 98: 
 99: 
100: 
101:     /**
102:      * Removes special controls characters and normalizes line endings and spaces.
103:      * @param  string  UTF-8 encoding or 8-bit
104:      * @return string
105:      */
106:     public static function normalize($s)
107:     {
108:         // standardize line endings to unix-like
109:         $s = str_replace("\r\n", "\n", $s); // DOS
110:         $s = strtr($s, "\r", "\n"); // Mac
111: 
112:         // remove control characters; leave \t + \n
113:         $s = preg_replace('#[\x00-\x08\x0B-\x1F]+#', '', $s);
114: 
115:         // right trim
116:         $s = preg_replace("#[\t ]+$#m", '', $s);
117: 
118:         // trailing spaces
119:         $s = trim($s, "\n");
120: 
121:         return $s;
122:     }
123: 
124: 
125: 
126:     /**
127:      * Converts to web safe characters [a-z0-9-] text.
128:      * @param  string  UTF-8 encoding
129:      * @param  string  ASCII
130:      * @param  bool
131:      * @return string
132:      */
133:     public static function webalize($s, $charlist = NULL, $lower = TRUE)
134:     {
135:         $s = strtr($s, '`\'"^~', '-----');
136:         if (ICONV_IMPL === 'glibc') {
137:             $s = @iconv('UTF-8', 'WINDOWS-1250//TRANSLIT', $s); // intentionally @
138:             $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"
139:                 ."\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",
140:                 "ALLSSSSTZZZallssstzzzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTsraaaalccceeeeiiddnnooooruuuuyt");
141:         } else {
142:             $s = @iconv('UTF-8', 'ASCII//TRANSLIT', $s); // intentionally @
143:         }
144:         $s = str_replace(array('`', "'", '"', '^', '~'), '', $s);
145:         if ($lower) $s = strtolower($s);
146:         $s = preg_replace('#[^a-z0-9' . preg_quote($charlist, '#') . ']+#i', '-', $s);
147:         $s = trim($s, '-');
148:         return $s;
149:     }
150: 
151: 
152: 
153:     /**
154:      * Truncates string to maximal length.
155:      * @param  string  UTF-8 encoding
156:      * @param  int
157:      * @param  string  UTF-8 encoding
158:      * @return string
159:      */
160:     public static function truncate($s, $maxLen, $append = "\xE2\x80\xA6")
161:     {
162:         if (iconv_strlen($s, 'UTF-8') > $maxLen) {
163:             $maxLen = $maxLen - iconv_strlen($append, 'UTF-8');
164:             if ($maxLen < 1) {
165:                 return $append;
166: 
167:             } elseif (preg_match('#^.{1,'.$maxLen.'}(?=[\s\x00-/:-@\[-`{-~])#us', $s, $matches)) {
168:                 return $matches[0] . $append;
169: 
170:             } else {
171:                 return iconv_substr($s, 0, $maxLen, 'UTF-8') . $append;
172:             }
173:         }
174:         return $s;
175:     }
176: 
177: 
178: 
179:     /**
180:      * Indents the content from the left.
181:      * @param  string  UTF-8 encoding or 8-bit
182:      * @param  int
183:      * @param  string
184:      * @return string
185:      */
186:     public static function indent($s, $level = 1, $chars = "\t")
187:     {
188:         return $level < 1 ? $s : preg_replace('#(?:^|[\r\n]+)(?=[^\r\n])#', '$0' . str_repeat($chars, $level), $s);
189:     }
190: 
191: 
192: 
193:     /**
194:      * Convert to lower case.
195:      * @param  string  UTF-8 encoding
196:      * @return string
197:      */
198:     public static function lower($s)
199:     {
200:         return mb_strtolower($s, 'UTF-8');
201:     }
202: 
203: 
204: 
205:     /**
206:      * Convert to upper case.
207:      * @param  string  UTF-8 encoding
208:      * @return string
209:      */
210:     public static function upper($s)
211:     {
212:         return mb_strtoupper($s, 'UTF-8');
213:     }
214: 
215: 
216: 
217:     /**
218:      * Capitalize string.
219:      * @param  string  UTF-8 encoding
220:      * @return string
221:      */
222:     public static function capitalize($s)
223:     {
224:         return mb_convert_case($s, MB_CASE_TITLE, 'UTF-8');
225:     }
226: 
227: 
228: 
229:     /**
230:      * Strips whitespace.
231:      * @param  string  UTF-8 encoding
232:      * @param  string
233:      * @return string
234:      */
235:     public static function trim($s, $charlist = " \t\n\r\0\x0B\xC2\xA0")
236:     {
237:         $charlist = preg_quote($charlist, '#');
238:         return preg_replace('#^['.$charlist.']+|['.$charlist.']+$#u', '', $s);
239:     }
240: 
241: 
242: 
243:     /**
244:      * Pad a string to a certain length with another string.
245:      * @param  string  UTF-8 encoding
246:      * @param  int
247:      * @param  string
248:      * @return string
249:      */
250:     public static function padLeft($s, $length, $pad = ' ')
251:     {
252:         $length = max(0, $length - iconv_strlen($s, 'UTF-8'));
253:         $padLen = iconv_strlen($pad, 'UTF-8');
254:         return str_repeat($pad, $length / $padLen) . iconv_substr($pad, 0, $length % $padLen, 'UTF-8') . $s;
255:     }
256: 
257: 
258: 
259:     /**
260:      * Pad a string to a certain length with another string.
261:      * @param  string  UTF-8 encoding
262:      * @param  int
263:      * @param  string
264:      * @return string
265:      */
266:     public static function padRight($s, $length, $pad = ' ')
267:     {
268:         $length = max(0, $length - iconv_strlen($s, 'UTF-8'));
269:         $padLen = iconv_strlen($pad, 'UTF-8');
270:         return $s . str_repeat($pad, $length / $padLen) . iconv_substr($pad, 0, $length % $padLen, 'UTF-8');
271:     }
272: 
273: }
274: 
Nette Framework 0.9.7 (for PHP 5.2) API documentation generated by ApiGen 2.3.0