b30174956c1e57d1ccce1f96da8e7d0dabe6b5e6
[squirrelmail.git] / functions / decode / utf-8.php
1 <?php
2 /*
3 * decode/utf-8.php
4 * $Id$
5 *
6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This file contains utf-8 decoding function that is needed to read
10 * utf-8 encoded mails in non-utf-8 locale.
11 *
12 * Every decoded character consists of n bytes. First byte is octal
13 * 300-375, other bytes - always octals 200-277.
14 *
15 * \a\b characters are decoded to html code octdec(a-300)*64 + octdec(b-200)
16 * \a\b\c characters are decoded to html code octdec(a-340)*64*64 + octdec(b-200)*64 + octdec(c-200)
17 *
18 * decoding cycle is unfinished. please test and report problems to tokul@users.sourceforge.net
19 *
20 */
21 function charset_decode_utf8 ($string) {
22 global $default_charset, $languages, $sm_notAlias;
23
24 if (strtolower($default_charset) == 'utf-8')
25 return $string;
26 if (strtolower($languages[$sm_notAlias]['CHARSET']) == 'utf-8')
27 return $string;
28
29 /* Only do the slow convert if there are 8-bit characters */
30 if (! ereg("[\200-\377]", $string))
31 return $string;
32
33 // decode three byte unicode characters
34 $string = preg_replace("/([\340-\357])([\200-\277])([\200-\277])/e",
35 "'&#'.((ord('\\1')-224)*4096+(ord('\\2')-128)*64+(ord('\\3')-128)).';'",
36 $string);
37
38 // decode two byte unicode characters
39 $string = preg_replace("/([\300-\337])([\200-\277])/e",
40 "'&#'.((ord('\\1')-192)*64+(ord('\\2')-128)).';'",
41 $string);
42
43 return $string;
44 }
45
46 ?>