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 ********************************************************************************/
10 if (function_exists('dgettext')) {
13 return dgettext('banana', $str);
22 if (!function_exists('is_utf8')) {
25 return @iconv
('utf-8', 'utf-8', $s) == $s;
29 function banana_utf8entities($source)
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
38 // the number of bits to shift each charNum by
51 $len = strlen($source);
55 $charPos = $source{$pos};
56 $asciiPos = ord($charPos);
59 $encodedString .= $charPos;
65 if (($asciiPos >= 240) && ($asciiPos <= 255)) // 4 chars representing one unicode character
67 else if (($asciiPos >= 224) && ($asciiPos <= 239)) // 3 chars representing one unicode character
69 else if (($asciiPos >= 192) && ($asciiPos <= 223)) // 2 chars representing one unicode character
71 else // 1 char (lower ascii)
73 $thisLetter = substr($source, $pos, $i);
76 // process the string representing the letter to a unicode entity
77 $thisLen = strlen($thisLetter);
80 while ($thisPos < $thisLen)
82 $thisCharOrd = ord(substr($thisLetter, $thisPos, 1));
85 $charNum = intval($thisCharOrd - $decrement[$thisLen]);
86 $decimalCode +
= ($charNum << $shift[$thisLen][$thisPos]);
90 $charNum = intval($thisCharOrd - 128);
91 $decimalCode +
= ($charNum << $shift[$thisLen][$thisPos]);
97 $encodedLetter = '&#'. str_pad($decimalCode, ($thisLen==1)?
3:5, '0', STR_PAD_LEFT
).';';
98 $encodedString .= $encodedLetter;
101 return $encodedString;
104 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: