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_entities($source)
31 if (is_string($source)) {
32 return banana_htmlentities($source);
38 function banana_utf8entities($source)
40 // array used to figure what number to decrement from character order value
41 // according to number of characters used to map unicode to ascii by utf-8
47 // the number of bits to shift each charNum by
60 $len = strlen($source);
64 $charPos = $source{$pos};
65 $asciiPos = ord($charPos);
68 $encodedString .= $charPos;
74 if (($asciiPos >= 240) && ($asciiPos <= 255)) // 4 chars representing one unicode character
76 else if (($asciiPos >= 224) && ($asciiPos <= 239)) // 3 chars representing one unicode character
78 else if (($asciiPos >= 192) && ($asciiPos <= 223)) // 2 chars representing one unicode character
80 else // 1 char (lower ascii)
82 $thisLetter = substr($source, $pos, $i);
85 // process the string representing the letter to a unicode entity
86 $thisLen = strlen($thisLetter);
89 while ($thisPos < $thisLen)
91 $thisCharOrd = ord(substr($thisLetter, $thisPos, 1));
94 $charNum = intval($thisCharOrd - $decrement[$thisLen]);
95 $decimalCode +
= ($charNum << $shift[$thisLen][$thisPos]);
99 $charNum = intval($thisCharOrd - 128);
100 $decimalCode +
= ($charNum << $shift[$thisLen][$thisPos]);
106 $encodedLetter = '&#'. str_pad($decimalCode, ($thisLen==1)?
3:5, '0', STR_PAD_LEFT
).';';
107 $encodedString .= $encodedLetter;
110 return $encodedString;
113 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: