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