1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12:
13:
14: 15: 16: 17: 18: 19:
20: class TexyUtf
21: {
22:
23: private static $xlat;
24:
25:
26: private static $xlatCache;
27:
28:
29:
30: 31: 32:
33: final public function __construct()
34: {
35: throw new LogicException("Cannot instantiate static class " . get_class($this));
36: }
37:
38:
39:
40: 41: 42:
43: public static function toUtf($s, $encoding)
44: {
45: return iconv($encoding, 'UTF-8', $s);
46: }
47:
48:
49:
50: 51: 52:
53: public static function utfTo($s, $encoding)
54: {
55: return iconv('utf-8', $encoding.'//TRANSLIT', $s);
56: }
57:
58:
59:
60: 61: 62:
63: public static function strtolower($s)
64: {
65: if (function_exists('mb_strtolower'))
66: return mb_strtolower($s, 'UTF-8');
67:
68: return @iconv('WINDOWS-1250', 'UTF-8', strtr( 69: iconv('UTF-8', 'WINDOWS-1250//IGNORE', $s),
70: "ABCDEFGHIJKLMNOPQRSTUVWXYZ\x8a\x8c\x8d\x8e\x8f\xa3\xa5\xaa\xaf\xbc\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd\xde",
71: "abcdefghijklmnopqrstuvwxyz\x9a\x9c\x9d\x9e\x9f\xb3\xb9\xba\xbf\xbe\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"
72: ));
73: }
74:
75:
76:
77: 78: 79: 80:
81: public static function utf2ascii($s)
82: {
83: $s = strtr($s, '`\'"^~', '-----');
84: if (ICONV_IMPL === 'glibc') {
85: $s = @iconv('UTF-8', 'WINDOWS-1250//TRANSLIT', $s); 86: $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"
87: ."\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",
88: "ALLSSSSTZZZallssstzzzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTsraaaalccceeeeiiddnnooooruuuuyt");
89: } else {
90: $s = @iconv('UTF-8', 'ASCII//TRANSLIT', $s); 91: }
92: $s = str_replace(array('`', "'", '"', '^', '~'), '', $s);
93: return $s;
94: }
95:
96:
97:
98: 99: 100:
101: public static function utf2html($s, $encoding)
102: {
103: 104: if (strcasecmp($encoding, 'utf-8') === 0) return $s;
105:
106: 107: self::$xlat = & self::$xlatCache[strtolower($encoding)];
108: if (!self::$xlat) {
109: for ($i = 128; $i<256; $i++) {
110: $ch = @iconv($encoding, 'UTF-8//IGNORE', chr($i)); 111: if ($ch) self::$xlat[$ch] = chr($i);
112: }
113: }
114:
115: 116: return preg_replace_callback('#[\x80-\x{FFFF}]#u', array(__CLASS__, 'cb'), $s);
117: }
118:
119:
120:
121: 122: 123:
124: private static function cb($m)
125: {
126: $m = $m[0];
127: if (isset(self::$xlat[$m])) return self::$xlat[$m];
128:
129: $ch1 = ord($m[0]);
130: $ch2 = ord($m[1]);
131: if (($ch2 >> 6) !== 2) return '';
132:
133: if (($ch1 & 0xE0) === 0xC0)
134: return '&#' . ((($ch1 & 0x1F) << 6) + ($ch2 & 0x3F)) . ';';
135:
136: if (($ch1 & 0xF0) === 0xE0) {
137: $ch3 = ord($m[2]);
138: if (($ch3 >> 6) !== 2) return '';
139: return '&#' . ((($ch1 & 0xF) << 12) + (($ch2 & 0x3F) << 06) + (($ch3 & 0x3F))) . ';';
140: }
141:
142: return '';
143: }
144:
145: }
146: