1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: final class NString
22: {
23:
24: 25: 26:
27: final public function __construct()
28: {
29: throw new LogicException("Cannot instantiate static class " . get_class($this));
30: }
31:
32:
33:
34: 35: 36: 37: 38: 39:
40: public static function checkEncoding($s, $encoding = 'UTF-8')
41: {
42: return $s === self::fixEncoding($s, $encoding);
43: }
44:
45:
46:
47: 48: 49: 50: 51: 52:
53: public static function fixEncoding($s, $encoding = 'UTF-8')
54: {
55:
56: $s = @iconv('UTF-16', $encoding . '//IGNORE', iconv($encoding, 'UTF-16//IGNORE', $s));
57: return str_replace("\xEF\xBB\xBF", '', $s);
58: }
59:
60:
61:
62: 63: 64: 65: 66: 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: 77: 78: 79: 80:
81: public static function startsWith($haystack, $needle)
82: {
83: return strncmp($haystack, $needle, strlen($needle)) === 0;
84: }
85:
86:
87:
88: 89: 90: 91: 92: 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: 103: 104: 105:
106: public static function normalize($s)
107: {
108:
109: $s = str_replace("\r\n", "\n", $s);
110: $s = strtr($s, "\r", "\n");
111:
112:
113: $s = preg_replace('#[\x00-\x08\x0B-\x1F]+#', '', $s);
114:
115:
116: $s = preg_replace("#[\t ]+$#m", '', $s);
117:
118:
119: $s = trim($s, "\n");
120:
121: return $s;
122: }
123:
124:
125:
126: 127: 128: 129: 130: 131: 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);
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);
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: 155: 156: 157: 158: 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: 181: 182: 183: 184: 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: 195: 196: 197:
198: public static function lower($s)
199: {
200: return mb_strtolower($s, 'UTF-8');
201: }
202:
203:
204:
205: 206: 207: 208: 209:
210: public static function upper($s)
211: {
212: return mb_strtoupper($s, 'UTF-8');
213: }
214:
215:
216:
217: 218: 219: 220: 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: 231: 232: 233: 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: 245: 246: 247: 248: 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: 261: 262: 263: 264: 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: