-<?php\r
-/********************************************************************************\r
-* banana/text.php : text tools\r
-* ---------------\r
-*\r
-* This file is part of the banana distribution\r
-* Copyright: See COPYING files that comes with this distribution\r
-********************************************************************************/\r
-\r
-if (function_exists('dgettext')) {\r
- function _b_($str)\r
- {\r
- return dgettext('banana', $str);\r
- }\r
-} else {\r
- function _b_($str)\r
- {\r
- return $str;\r
- }\r
-}\r
-\r
-if (!function_exists('is_utf8')) {\r
- function is_utf8($s)\r
- {\r
- return @iconv('utf-8', 'utf-8', $s) == $s;\r
- }\r
-}\r
-\r
-function banana_entities($source)\r
-{\r
- if (is_string($source)) {\r
- return banana_htmlentities($source);\r
- } else {\r
- return $source;\r
- }\r
-}\r
-\r
-function banana_utf8entities($source)\r
-{\r
- // array used to figure what number to decrement from character order value \r
- // according to number of characters used to map unicode to ascii by utf-8\r
- $decrement[4] = 240;\r
- $decrement[3] = 224;\r
- $decrement[2] = 192;\r
- $decrement[1] = 0;\r
- \r
- // the number of bits to shift each charNum by\r
- $shift[1][0] = 0;\r
- $shift[2][0] = 6;\r
- $shift[2][1] = 0;\r
- $shift[3][0] = 12;\r
- $shift[3][1] = 6;\r
- $shift[3][2] = 0;\r
- $shift[4][0] = 18;\r
- $shift[4][1] = 12;\r
- $shift[4][2] = 6;\r
- $shift[4][3] = 0;\r
- \r
- $pos = 0;\r
- $len = strlen($source);\r
- $encodedString = '';\r
- while ($pos < $len)\r
- {\r
- $charPos = $source{$pos};\r
- $asciiPos = ord($charPos);\r
- if ($asciiPos < 128)\r
- {\r
- $encodedString .= $charPos;\r
- $pos++;\r
- continue;\r
- }\r
- \r
- $i=1;\r
- if (($asciiPos >= 240) && ($asciiPos <= 255)) // 4 chars representing one unicode character\r
- $i=4;\r
- else if (($asciiPos >= 224) && ($asciiPos <= 239)) // 3 chars representing one unicode character\r
- $i=3;\r
- else if (($asciiPos >= 192) && ($asciiPos <= 223)) // 2 chars representing one unicode character\r
- $i=2;\r
- else // 1 char (lower ascii)\r
- $i=1;\r
- $thisLetter = substr($source, $pos, $i);\r
- $pos += $i;\r
- \r
- // process the string representing the letter to a unicode entity\r
- $thisLen = strlen($thisLetter);\r
- $thisPos = 0;\r
- $decimalCode = 0;\r
- while ($thisPos < $thisLen)\r
- {\r
- $thisCharOrd = ord(substr($thisLetter, $thisPos, 1));\r
- if ($thisPos == 0)\r
- {\r
- $charNum = intval($thisCharOrd - $decrement[$thisLen]);\r
- $decimalCode += ($charNum << $shift[$thisLen][$thisPos]);\r
- }\r
- else\r
- {\r
- $charNum = intval($thisCharOrd - 128);\r
- $decimalCode += ($charNum << $shift[$thisLen][$thisPos]);\r
- }\r
- \r
- $thisPos++;\r
- }\r
- \r
- $encodedLetter = '&#'. str_pad($decimalCode, ($thisLen==1)?3:5, '0', STR_PAD_LEFT).';';\r
- $encodedString .= $encodedLetter;\r
- }\r
- \r
- return $encodedString;\r
-}\r
-\r
-// vim:set et sw=4 sts=4 ts=4 enc=utf-8:\r
-?>\r
+<?php
+/********************************************************************************
+* banana/text.php : text tools
+* ---------------
+*
+* This file is part of the banana distribution
+* Copyright: See COPYING files that comes with this distribution
+********************************************************************************/
+
+if (function_exists('dgettext')) {
+ function _b_($str)
+ {
+ return dgettext('banana', $str);
+ }
+} else {
+ function _b_($str)
+ {
+ return $str;
+ }
+}
+
+if (!function_exists('is_utf8')) {
+ function is_utf8($s)
+ {
+ return @iconv('utf-8', 'utf-8', $s) == $s;
+ }
+}
+
+function banana_entities($source)
+{
+ if (is_string($source)) {
+ return banana_htmlentities($source);
+ } else {
+ return $source;
+ }
+}
+
+function banana_utf8entities($source)
+{
+ // array used to figure what number to decrement from character order value
+ // according to number of characters used to map unicode to ascii by utf-8
+ $decrement[4] = 240;
+ $decrement[3] = 224;
+ $decrement[2] = 192;
+ $decrement[1] = 0;
+
+ // the number of bits to shift each charNum by
+ $shift[1][0] = 0;
+ $shift[2][0] = 6;
+ $shift[2][1] = 0;
+ $shift[3][0] = 12;
+ $shift[3][1] = 6;
+ $shift[3][2] = 0;
+ $shift[4][0] = 18;
+ $shift[4][1] = 12;
+ $shift[4][2] = 6;
+ $shift[4][3] = 0;
+
+ $pos = 0;
+ $len = strlen($source);
+ $encodedString = '';
+ while ($pos < $len)
+ {
+ $charPos = $source{$pos};
+ $asciiPos = ord($charPos);
+ if ($asciiPos < 128)
+ {
+ $encodedString .= $charPos;
+ $pos++;
+ continue;
+ }
+
+ $i=1;
+ if (($asciiPos >= 240) && ($asciiPos <= 255)) // 4 chars representing one unicode character
+ $i=4;
+ else if (($asciiPos >= 224) && ($asciiPos <= 239)) // 3 chars representing one unicode character
+ $i=3;
+ else if (($asciiPos >= 192) && ($asciiPos <= 223)) // 2 chars representing one unicode character
+ $i=2;
+ else // 1 char (lower ascii)
+ $i=1;
+ $thisLetter = substr($source, $pos, $i);
+ $pos += $i;
+
+ // process the string representing the letter to a unicode entity
+ $thisLen = strlen($thisLetter);
+ $thisPos = 0;
+ $decimalCode = 0;
+ while ($thisPos < $thisLen)
+ {
+ $thisCharOrd = ord(substr($thisLetter, $thisPos, 1));
+ if ($thisPos == 0)
+ {
+ $charNum = intval($thisCharOrd - $decrement[$thisLen]);
+ $decimalCode += ($charNum << $shift[$thisLen][$thisPos]);
+ }
+ else
+ {
+ $charNum = intval($thisCharOrd - 128);
+ $decimalCode += ($charNum << $shift[$thisLen][$thisPos]);
+ }
+
+ $thisPos++;
+ }
+
+ $encodedLetter = '&#'. str_pad($decimalCode, ($thisLen==1)?3:5, '0', STR_PAD_LEFT).';';
+ $encodedString .= $encodedLetter;
+ }
+
+ return $encodedString;
+}
+
+// vim:set et sw=4 sts=4 ts=4 enc=utf-8:
+?>