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