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