1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19:
20: final class NString
21: {
22:
23: 24: 25:
26: final public function __construct()
27: {
28: throw new LogicException("Cannot instantiate static class " . get_class($this));
29: }
30:
31:
32:
33: 34: 35: 36: 37: 38:
39: public static function checkEncoding($s, $encoding = 'UTF-8')
40: {
41: return $s === self::fixEncoding($s, $encoding);
42: }
43:
44:
45:
46: 47: 48: 49: 50: 51:
52: public static function fixEncoding($s, $encoding = 'UTF-8')
53: {
54: 55: return @iconv('UTF-16', $encoding . '//IGNORE', iconv($encoding, 'UTF-16//IGNORE', $s)); 56: }
57:
58:
59:
60: 61: 62: 63: 64: 65:
66: public static function chr($code, $encoding = 'UTF-8')
67: {
68: return iconv('UTF-32BE', $encoding . '//IGNORE', pack('N', $code));
69: }
70:
71:
72:
73: 74: 75: 76: 77: 78:
79: public static function startsWith($haystack, $needle)
80: {
81: return strncmp($haystack, $needle, strlen($needle)) === 0;
82: }
83:
84:
85:
86: 87: 88: 89: 90: 91:
92: public static function endsWith($haystack, $needle)
93: {
94: return strlen($needle) === 0 || substr($haystack, -strlen($needle)) === $needle;
95: }
96:
97:
98:
99: 100: 101: 102: 103:
104: public static function normalize($s)
105: {
106: 107: $s = str_replace("\r\n", "\n", $s); 108: $s = strtr($s, "\r", "\n"); 109:
110: 111: $s = preg_replace('#[\x00-\x08\x0B-\x1F]+#', '', $s);
112:
113: 114: $s = preg_replace("#[\t ]+$#m", '', $s);
115:
116: 117: $s = trim($s, "\n");
118:
119: return $s;
120: }
121:
122:
123:
124: 125: 126: 127: 128:
129: public static function toAscii($s)
130: {
131: $s = preg_replace('#[^\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}]#u', '', $s);
132: $s = strtr($s, '`\'"^~', "\x01\x02\x03\x04\x05");
133: if (ICONV_IMPL === 'glibc') {
134: $s = @iconv('UTF-8', 'WINDOWS-1250//TRANSLIT', $s); 135: $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"
136: ."\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",
137: "ALLSSSSTZZZallssstzzzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTsraaaalccceeeeiiddnnooooruuuuyt");
138: } else {
139: $s = @iconv('UTF-8', 'ASCII//TRANSLIT', $s); 140: }
141: $s = str_replace(array('`', "'", '"', '^', '~'), '', $s);
142: return strtr($s, "\x01\x02\x03\x04\x05", '`\'"^~');
143: }
144:
145:
146:
147: 148: 149: 150: 151: 152: 153:
154: public static function webalize($s, $charlist = NULL, $lower = TRUE)
155: {
156: $s = self::toAscii($s);
157: if ($lower) $s = strtolower($s);
158: $s = preg_replace('#[^a-z0-9' . preg_quote($charlist, '#') . ']+#i', '-', $s);
159: $s = trim($s, '-');
160: return $s;
161: }
162:
163:
164:
165: 166: 167: 168: 169: 170: 171:
172: public static function truncate($s, $maxLen, $append = "\xE2\x80\xA6")
173: {
174: if (self::length($s) > $maxLen) {
175: $maxLen = $maxLen - self::length($append);
176: if ($maxLen < 1) {
177: return $append;
178:
179: } elseif ($matches = self::match($s, '#^.{1,'.$maxLen.'}(?=[\s\x00-@\[-`{-~])#us')) {
180: return $matches[0] . $append;
181:
182: } else {
183: return iconv_substr($s, 0, $maxLen, 'UTF-8') . $append;
184: }
185: }
186: return $s;
187: }
188:
189:
190:
191: 192: 193: 194: 195: 196: 197:
198: public static function indent($s, $level = 1, $chars = "\t")
199: {
200: return $level < 1 ? $s : self::replace($s, '#(?:^|[\r\n]+)(?=[^\r\n])#', '$0' . str_repeat($chars, $level));
201: }
202:
203:
204:
205: 206: 207: 208: 209:
210: public static function lower($s)
211: {
212: return mb_strtolower($s, 'UTF-8');
213: }
214:
215:
216:
217: 218: 219: 220: 221:
222: public static function upper($s)
223: {
224: return mb_strtoupper($s, 'UTF-8');
225: }
226:
227:
228:
229: 230: 231: 232: 233:
234: public static function capitalize($s)
235: {
236: return mb_convert_case($s, MB_CASE_TITLE, 'UTF-8');
237: }
238:
239:
240:
241: 242: 243: 244: 245: 246: 247:
248: public static function compare($left, $right, $len = NULL)
249: {
250: if ($len < 0) {
251: $left = iconv_substr($left, $len, -$len, 'UTF-8');
252: $right = iconv_substr($right, $len, -$len, 'UTF-8');
253: } elseif ($len !== NULL) {
254: $left = iconv_substr($left, 0, $len, 'UTF-8');
255: $right = iconv_substr($right, 0, $len, 'UTF-8');
256: }
257: return self::lower($left) === self::lower($right);
258: }
259:
260:
261:
262: 263: 264: 265: 266:
267: public static function length($s)
268: {
269: return function_exists('mb_strlen') ? mb_strlen($s, 'UTF-8') : strlen(utf8_decode($s));
270: }
271:
272:
273:
274: 275: 276: 277: 278: 279:
280: public static function trim($s, $charlist = " \t\n\r\0\x0B\xC2\xA0")
281: {
282: $charlist = preg_quote($charlist, '#');
283: return self::replace($s, '#^['.$charlist.']+|['.$charlist.']+$#u', '');
284: }
285:
286:
287:
288: 289: 290: 291: 292: 293: 294:
295: public static function padLeft($s, $length, $pad = ' ')
296: {
297: $length = max(0, $length - self::length($s));
298: $padLen = self::length($pad);
299: return str_repeat($pad, $length / $padLen) . iconv_substr($pad, 0, $length % $padLen, 'UTF-8') . $s;
300: }
301:
302:
303:
304: 305: 306: 307: 308: 309: 310:
311: public static function padRight($s, $length, $pad = ' ')
312: {
313: $length = max(0, $length - self::length($s));
314: $padLen = self::length($pad);
315: return $s . str_repeat($pad, $length / $padLen) . iconv_substr($pad, 0, $length % $padLen, 'UTF-8');
316: }
317:
318:
319:
320: 321: 322: 323: 324: 325: 326:
327: public static function split($subject, $pattern, $flags = 0)
328: {
329: NDebug::tryError();
330: $res = preg_split($pattern, $subject, -1, $flags | PREG_SPLIT_DELIM_CAPTURE);
331: self::catchPregError($pattern);
332: return $res;
333: }
334:
335:
336:
337: 338: 339: 340: 341: 342: 343: 344:
345: public static function match($subject, $pattern, $flags = 0, $offset = 0)
346: {
347: NDebug::tryError();
348: $res = preg_match($pattern, $subject, $m, $flags, $offset);
349: self::catchPregError($pattern);
350: if ($res) {
351: return $m;
352: }
353: }
354:
355:
356:
357: 358: 359: 360: 361: 362: 363: 364:
365: public static function matchAll($subject, $pattern, $flags = 0, $offset = 0)
366: {
367: NDebug::tryError();
368: $res = preg_match_all($pattern, $subject, $m, ($flags & PREG_PATTERN_ORDER) ? $flags : ($flags | PREG_SET_ORDER), $offset);
369: self::catchPregError($pattern);
370: return $m;
371: }
372:
373:
374:
375: 376: 377: 378: 379: 380: 381: 382:
383: public static function replace($subject, $pattern, $replacement = NULL, $limit = -1)
384: {
385: NDebug::tryError();
386: if (is_object($replacement) || is_array($replacement)) {
387: if ($replacement instanceof NCallback) {
388: $replacement = $replacement->getNative();
389: }
390: if (!is_callable($replacement, FALSE, $textual)) {
391: NDebug::catchError($foo);
392: throw new InvalidStateException("Callback '$textual' is not callable.");
393: }
394: $res = preg_replace_callback($pattern, $replacement, $subject, $limit);
395:
396: } elseif (is_array($pattern)) {
397: $res = preg_replace(array_keys($pattern), array_values($pattern), $subject, $limit);
398:
399: } else {
400: $res = preg_replace($pattern, $replacement, $subject, $limit);
401: }
402: self::catchPregError($pattern);
403: return $res;
404: }
405:
406:
407:
408:
409: public static function catchPregError($pattern)
410: {
411: if (NDebug::catchError($message)) { 412: throw new NRegexpException("$message in pattern: $pattern");
413:
414: } elseif (preg_last_error()) { 415: static $messages = array(
416: PREG_INTERNAL_ERROR => 'Internal error',
417: PREG_BACKTRACK_LIMIT_ERROR => 'Backtrack limit was exhausted',
418: PREG_RECURSION_LIMIT_ERROR => 'Recursion limit was exhausted',
419: PREG_BAD_UTF8_ERROR => 'Malformed UTF-8 data',
420: 5 => 'Offset didn\'t correspond to the begin of a valid UTF-8 code point', 421: );
422: $code = preg_last_error();
423: throw new NRegexpException((isset($messages[$code]) ? $messages[$code] : 'Unknown error') . " (pattern: $pattern)", $code);
424: }
425: }
426:
427: }
428:
429:
430:
431: 432: 433:
434: class NRegexpException extends Exception
435: {
436: }
437: