Fix banana stand-alone
[banana.git] / banana / text.func.inc.php
1 <?php
2 /********************************************************************************
3 * banana/text.php : text tools
4 * ---------------
5 *
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
9
10 if (function_exists('dgettext')) {
11 function _b_($str)
12 {
13 return dgettext('banana', $str);
14 }
15 } else {
16 function _b_($str)
17 {
18 return $str;
19 }
20 }
21
22 if (!function_exists('is_utf8')) {
23 function is_utf8($s)
24 {
25 return @iconv('utf-8', 'utf-8', $s) == $s;
26 }
27 }
28
29 function banana_utf8entities($source)
30 {
31 // array used to figure what number to decrement from character order value
32 // according to number of characters used to map unicode to ascii by utf-8
33 $decrement[4] = 240;
34 $decrement[3] = 224;
35 $decrement[2] = 192;
36 $decrement[1] = 0;
37
38 // the number of bits to shift each charNum by
39 $shift[1][0] = 0;
40 $shift[2][0] = 6;
41 $shift[2][1] = 0;
42 $shift[3][0] = 12;
43 $shift[3][1] = 6;
44 $shift[3][2] = 0;
45 $shift[4][0] = 18;
46 $shift[4][1] = 12;
47 $shift[4][2] = 6;
48 $shift[4][3] = 0;
49
50 $pos = 0;
51 $len = strlen($source);
52 $encodedString = '';
53 while ($pos < $len)
54 {
55 $charPos = $source{$pos};
56 $asciiPos = ord($charPos);
57 if ($asciiPos < 128)
58 {
59 $encodedString .= $charPos;
60 $pos++;
61 continue;
62 }
63
64 $i=1;
65 if (($asciiPos >= 240) && ($asciiPos <= 255)) // 4 chars representing one unicode character
66 $i=4;
67 else if (($asciiPos >= 224) && ($asciiPos <= 239)) // 3 chars representing one unicode character
68 $i=3;
69 else if (($asciiPos >= 192) && ($asciiPos <= 223)) // 2 chars representing one unicode character
70 $i=2;
71 else // 1 char (lower ascii)
72 $i=1;
73 $thisLetter = substr($source, $pos, $i);
74 $pos += $i;
75
76 // process the string representing the letter to a unicode entity
77 $thisLen = strlen($thisLetter);
78 $thisPos = 0;
79 $decimalCode = 0;
80 while ($thisPos < $thisLen)
81 {
82 $thisCharOrd = ord(substr($thisLetter, $thisPos, 1));
83 if ($thisPos == 0)
84 {
85 $charNum = intval($thisCharOrd - $decrement[$thisLen]);
86 $decimalCode += ($charNum << $shift[$thisLen][$thisPos]);
87 }
88 else
89 {
90 $charNum = intval($thisCharOrd - 128);
91 $decimalCode += ($charNum << $shift[$thisLen][$thisPos]);
92 }
93
94 $thisPos++;
95 }
96
97 $encodedLetter = '&#'. str_pad($decimalCode, ($thisLen==1)?3:5, '0', STR_PAD_LEFT).';';
98 $encodedString .= $encodedLetter;
99 }
100
101 return $encodedString;
102 }
103
104 // vim:set et sw=4 sts=4 ts=4 enc=utf-8:
105 ?>