2 /********************************************************************************
3 * banana/text.php : text tools
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
13 $str = utf8_encode($str);
15 return dgettext('banana', $str);
18 if (!function_exists('is_utf8')) {
21 return @iconv
('utf-8', 'utf-8', $s) == $s;
25 function banana_utf8entities($source)
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
34 // the number of bits to shift each charNum by
47 $len = strlen($source);
51 $charPos = $source{$pos};
52 $asciiPos = ord($charPos);
55 $encodedString .= $charPos;
61 if (($asciiPos >= 240) && ($asciiPos <= 255)) // 4 chars representing one unicode character
63 else if (($asciiPos >= 224) && ($asciiPos <= 239)) // 3 chars representing one unicode character
65 else if (($asciiPos >= 192) && ($asciiPos <= 223)) // 2 chars representing one unicode character
67 else // 1 char (lower ascii)
69 $thisLetter = substr($source, $pos, $i);
72 // process the string representing the letter to a unicode entity
73 $thisLen = strlen($thisLetter);
76 while ($thisPos < $thisLen)
78 $thisCharOrd = ord(substr($thisLetter, $thisPos, 1));
81 $charNum = intval($thisCharOrd - $decrement[$thisLen]);
82 $decimalCode +
= ($charNum << $shift[$thisLen][$thisPos]);
86 $charNum = intval($thisCharOrd - 128);
87 $decimalCode +
= ($charNum << $shift[$thisLen][$thisPos]);
93 $encodedLetter = '&#'. str_pad($decimalCode, ($thisLen==1)?
3:5, '0', STR_PAD_LEFT
).';';
94 $encodedString .= $encodedLetter;
97 return $encodedString;
100 // vim:set et sw=4 sts=4 ts=4