UTF-8 conversion functions designed for use in PHP-GTK apps Adam Rambousek - rambousek@volny.cz version history: 1.03 --- 12/02/2002 * added Win1257 support 1.02 --- 30/11/2001 * added ISO8859-1 support 1.01 * added Win1250 support 1.00 string to_utf8(string string [, string charset]) string from_utf8(string string [, string charset]) supported charsets: name of charset you must use in script ISO8859-2: iso2 (this is the default charset, you don't have to specify it) Windows1250: win1250 ISO8859-1: iso1 Windows1257: win1257 example: $new_string=to_utf8($some_string,"win1250"); */ /* translation table - actually, it's array where key is hexadecimal number of character in ISO8859-2/Windows1250 and value is its two byte representation in UTF-8 */ class Encoding { public array $CharTable; function __construct() { $this->CharTable = array( 'iso2' => array( 0x80=>"\xc2\x80", 0x81=>"\xc2\x81", 0x82=>"\xc2\x82", 0x83=>"\xc2\x83", 0x84=>"\xc2\x84", 0x85=>"\xc2\x85", 0x86=>"\xc2\x86", 0x87=>"\xc2\x87", 0x88=>"\xc2\x88", 0x89=>"\xc2\x89", 0x8A=>"\xc2\x8a", 0x8B=>"\xc2\x8b", 0x8C=>"\xc2\x8c", 0x8D=>"\xc2\x8d", 0x8E=>"\xc2\x8e", 0x8F=>"\xc2\x8f", 0x90=>"\xc2\x90", 0x91=>"\xc2\x91", 0x92=>"\xc2\x92", 0x93=>"\xc2\x93", 0x94=>"\xc2\x94", 0x95=>"\xc2\x95", 0x96=>"\xc2\x96", 0x97=>"\xc2\x97", 0x98=>"\xc2\x98", 0x99=>"\xc2\x99", 0x9A=>"\xc2\x9a", 0x9B=>"\xc2\x9b", 0x9C=>"\xc2\x9c", 0x9D=>"\xc2\x9d", 0x9E=>"\xc2\x9e", 0x9F=>"\xc2\x9f", 0xA0=>"\xc2\xa0", 0xA1=>"\xc4\x84", 0xA2=>"\xcb\x98", 0xA3=>"\xc5\x81", 0xA4=>"\xc2\xa4", 0xA5=>"\xc4\xbd", 0xA6=>"\xc5\x9a", 0xA7=>"\xc2\xa7", 0xA8=>"\xc2\xa8", 0xA9=>"\xc5\xa0", 0xAA=>"\x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\xc4\x8d", 0xE9=>"\xc3\xa9", 0xEA=>"\xc4\x99", 0xEB=>"\xc3\xab", 0xEC=>"\xc4\x9b", 0xED=>"\xc3\xad", 0xEE=>"\xc3\xae", 0xEF=>"\xc4\x8f", 0xF0=>"\xc4\x91", 0xF1=>"\xc5\x84", 0xF2=>"\xc5\x88", 0xF3=>"\xc3\xb3", 0xF4=>"\xc3\xb4", 0xF5=>"\xc5\x91", 0xF6=>"\xc3\xb6", 0xF7=>"\xc3\xb7", 0xF8=>"\xc5\x99", 0xF9=>"\xc5\xaf", 0xFA=>"\xc3\xba", 0xFB=>"\xc5\xb1", 0xFC=>"\xc3\xbc", 0xFD=>"\xc3\xbd", 0xFE=>"\xc5\xa3", 0xFF=>"\xcb\x99" ), 'win1250' => array( 0x80=>"\xc2\x80", 0x81=>"\xc2\x81", 0x82=>"\xe2\x80\x9a", 0x83=>"\xc2\x83", 0x84=>"\xe2\x80\x9e", 0x85=>"\xe2\x80\xa6", 0x86=>"\xe2\x80\xa0", 0x87=>"\xe2\x80\xa1", 0x88=>"\xc2\x88", 0x89=>"\xe2\x80\xb0", 0x8a=>"\xc5\xa0", 0x8b=>"\xe2\x80\xb9", 0x8c=>"\xc5\x9a", 0x8d=>"\xc5\xa4", 0x8e=>"\xc5\xbd", 0x8f=>"\xc5\xb9", 0x90=>"\xc2\x90", 0x91=>"\xe2\x80\x98", 0x92=>"\xe2\x80\x99", 0x93=>"\xe2\x80\x9c", 0x94=>"\xe2\x80\x9d", 0x95=>"\xe2\x80\xa2", 0x96=>"\xe2\x80\x93", 0x97=>"\xe2\x80\x94", 0x98=>"\xe2\x80\x98", 0x99=>"\xe2\x84\xa2", 0x9a=>"\xc5\xa1", 0x9b=>"\xe2\x80\xba", 0x9c=>"\xc5\x9b", 0x9d=>"\xc5\xa5", 0x9e=>"\xc5\xbe", 0x9f=>"\xc5\xba", 0xa0=>"\xc2\xa0", 0xa1=>"\xcb\x87", 0xa2=>"\xcb\x98", 0xa3=>"\xc5\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 0xFD=>"\xc3\xbd", 0xFE=>"\xc3\xbe" ), 'win1257' => array( 0x80=>"\xe2\x82\xac", 0x81=>"\xc0\x4", 0x82=>"\xe2\x80\x9a", 0x83=>"\xc0\x4", 0x84=>"\xe2\x80\x9e", 0x85=>"\xe2\x80\xa6", 0x86=>"\xe2\x80\xa0", 0x87=>"\xe2\x80\xa1", 0x88=>"\xc0\x4", 0x89=>"\xe2\x80\xb0", 0x8A=>"\xc0\x4", 0x8B=>"\xe2\x80\xb9", 0x8C=>"\xc0\x4", 0x8D=>"\xc2\xa8", 0x8E=>"\xcb\x87", 0x8F=>"\xc2\xb8", 0x90=>"\xc0\x4", 0x91=>"\xe2\x80\x98", 0x92=>"\xe2\x80\x99", 0x93=>"\xe2\x80\x9c", 0x94=>"\xe2\x80\x9d", 0x95=>"\xe2\x80\xa2", 0x96=>"\xe2\x80\x93", 0x97=>"\xe2\x80\x94", 0x98=>"\xc0\x4", 0x99=>"\xe2\x84\xa2", 0x9A=>"\xc0\x4", 0x9B=>"\xe2\x80\xba", 0x9C=>"\xc0\x4", 0x9D=>"\xc2\xaf", 0x9E=>"\xcb\x9b", 0x9F=>"\xc0\x4", 0xA0=>"\xc2\xa0", 0xA1=>"\xc0\x4", 0xA2=>"\xc2\xa2", 0xA3=>"\xc2\xa3", 0xA4=>"\xc2\xa4", 0xA5=>"\xc0\x4", 0xA6=>"\xc2\xa6", 0xA7=>"\xc2\xa7", 0xA8=>"\xc3\x98", 0xA9=>"\xc2\xa9", 0xAA=>"\xc5\x96", 0xAB=>"\xc2\xab", 0xAC=>"\xc2\xac", 0xAD=>"\xc2\xad", 0xAE=>"\xc2\xae", 0xAF=>"\xc3\x86", 0xB0=>"\xc2\xb0", 0xB1=>"\xc2\xb1", 0xB2=>"\xc2\xb2", 0xB3=>"\xc2\xb3", 0xB4=>"\xc2\xb4", 0xB5=>"\xc2\xb5", 0xB6=>"\xc2\xb6", 0xB7=>"\xc2\xb7", 0xB8=>"\xc3\xb8", 0xB9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7=>"\xc3\xb7", 0xF8=>"\xc5\xb3", 0xF9=>"\xc5\x82", 0xFA=>"\xc5\x9b", 0xFB=>"\xc5\xab", 0xFC=>"\xc3\xbc", 0xFD=>"\xc5\xbc", 0xFE=>"\xc5\xbe", 0xFF=>"\xcb\x99" ), ); } function ToUTF8(string $String, string $Charset = 'iso2'): string { $Result = ''; for ($I = 0; $I < strlen($String); $I++) { if (ord($String[$I]) < 128) $Result .= $String[$I]; else if (ord($String[$I]) > 127) { $Result .= $this->CharTable[$Charset][ord($String[$I])]; } } return $Result; } function FromUTF8(string $String, string $Charset = 'iso2'): string { $Result = ''; $UTFPrefix = ''; for ($I = 0; $I < strlen($String); $I++) { if (ord($String[$I]) & 0x80) // UTF control character { if (ord($String[$I]) & 0x40) // First { if ($UTFPrefix != '') $Result .= chr(array_search($UTFPrefix, $this->CharTable[$Charset])); $UTFPrefix = $String[$I]; } else $UTFPrefix .= $String[$I]; // Next } else { if ($UTFPrefix != '') $Result .= chr(array_search($UTFPrefix, $this->CharTable[$Charset])); $UTFPrefix = ''; $Result .= $String[$I]; } } return $Result; } }