123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819 |
- <?php
- class PHPExcel_Shared_String
- {
-
-
-
- const STRING_REGEXP_FRACTION = '(-?)(\d+)\s+(\d+\/\d+)';
-
- private static $controlCharacters = array();
-
- private static $SYLKCharacters = array();
-
- private static $decimalSeparator;
-
- private static $thousandsSeparator;
-
- private static $currencyCode;
-
- private static $isMbstringEnabled;
-
- private static $isIconvEnabled;
-
- private static function buildControlCharacters()
- {
- for ($i = 0; $i <= 31; ++$i) {
- if ($i != 9 && $i != 10 && $i != 13) {
- $find = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_';
- $replace = chr($i);
- self::$controlCharacters[$find] = $replace;
- }
- }
- }
-
- private static function buildSYLKCharacters()
- {
- self::$SYLKCharacters = array(
- "\x1B 0" => chr(0),
- "\x1B 1" => chr(1),
- "\x1B 2" => chr(2),
- "\x1B 3" => chr(3),
- "\x1B 4" => chr(4),
- "\x1B 5" => chr(5),
- "\x1B 6" => chr(6),
- "\x1B 7" => chr(7),
- "\x1B 8" => chr(8),
- "\x1B 9" => chr(9),
- "\x1B :" => chr(10),
- "\x1B ;" => chr(11),
- "\x1B <" => chr(12),
- "\x1B :" => chr(13),
- "\x1B >" => chr(14),
- "\x1B ?" => chr(15),
- "\x1B!0" => chr(16),
- "\x1B!1" => chr(17),
- "\x1B!2" => chr(18),
- "\x1B!3" => chr(19),
- "\x1B!4" => chr(20),
- "\x1B!5" => chr(21),
- "\x1B!6" => chr(22),
- "\x1B!7" => chr(23),
- "\x1B!8" => chr(24),
- "\x1B!9" => chr(25),
- "\x1B!:" => chr(26),
- "\x1B!;" => chr(27),
- "\x1B!<" => chr(28),
- "\x1B!=" => chr(29),
- "\x1B!>" => chr(30),
- "\x1B!?" => chr(31),
- "\x1B'?" => chr(127),
- "\x1B(0" => '€',
- "\x1B(2" => '‚',
- "\x1B(3" => 'ƒ',
- "\x1B(4" => '„',
- "\x1B(5" => '…',
- "\x1B(6" => '†',
- "\x1B(7" => '‡',
- "\x1B(8" => 'ˆ',
- "\x1B(9" => '‰',
- "\x1B(:" => 'Š',
- "\x1B(;" => '‹',
- "\x1BNj" => 'Œ',
- "\x1B(>" => 'Ž',
- "\x1B)1" => '‘',
- "\x1B)2" => '’',
- "\x1B)3" => '“',
- "\x1B)4" => '”',
- "\x1B)5" => '•',
- "\x1B)6" => '–',
- "\x1B)7" => '—',
- "\x1B)8" => '˜',
- "\x1B)9" => '™',
- "\x1B):" => 'š',
- "\x1B);" => '›',
- "\x1BNz" => 'œ',
- "\x1B)>" => 'ž',
- "\x1B)?" => 'Ÿ',
- "\x1B*0" => ' ',
- "\x1BN!" => '¡',
- "\x1BN\"" => '¢',
- "\x1BN#" => '£',
- "\x1BN(" => '¤',
- "\x1BN%" => '¥',
- "\x1B*6" => '¦',
- "\x1BN'" => '§',
- "\x1BNH " => '¨',
- "\x1BNS" => '©',
- "\x1BNc" => 'ª',
- "\x1BN+" => '«',
- "\x1B*<" => '¬',
- "\x1B*=" => '',
- "\x1BNR" => '®',
- "\x1B*?" => '¯',
- "\x1BN0" => '°',
- "\x1BN1" => '±',
- "\x1BN2" => '²',
- "\x1BN3" => '³',
- "\x1BNB " => '´',
- "\x1BN5" => 'µ',
- "\x1BN6" => '¶',
- "\x1BN7" => '·',
- "\x1B+8" => '¸',
- "\x1BNQ" => '¹',
- "\x1BNk" => 'º',
- "\x1BN;" => '»',
- "\x1BN<" => '¼',
- "\x1BN=" => '½',
- "\x1BN>" => '¾',
- "\x1BN?" => '¿',
- "\x1BNAA" => 'À',
- "\x1BNBA" => 'Á',
- "\x1BNCA" => 'Â',
- "\x1BNDA" => 'Ã',
- "\x1BNHA" => 'Ä',
- "\x1BNJA" => 'Å',
- "\x1BNa" => 'Æ',
- "\x1BNKC" => 'Ç',
- "\x1BNAE" => 'È',
- "\x1BNBE" => 'É',
- "\x1BNCE" => 'Ê',
- "\x1BNHE" => 'Ë',
- "\x1BNAI" => 'Ì',
- "\x1BNBI" => 'Í',
- "\x1BNCI" => 'Î',
- "\x1BNHI" => 'Ï',
- "\x1BNb" => 'Ð',
- "\x1BNDN" => 'Ñ',
- "\x1BNAO" => 'Ò',
- "\x1BNBO" => 'Ó',
- "\x1BNCO" => 'Ô',
- "\x1BNDO" => 'Õ',
- "\x1BNHO" => 'Ö',
- "\x1B-7" => '×',
- "\x1BNi" => 'Ø',
- "\x1BNAU" => 'Ù',
- "\x1BNBU" => 'Ú',
- "\x1BNCU" => 'Û',
- "\x1BNHU" => 'Ü',
- "\x1B-=" => 'Ý',
- "\x1BNl" => 'Þ',
- "\x1BN{" => 'ß',
- "\x1BNAa" => 'à',
- "\x1BNBa" => 'á',
- "\x1BNCa" => 'â',
- "\x1BNDa" => 'ã',
- "\x1BNHa" => 'ä',
- "\x1BNJa" => 'å',
- "\x1BNq" => 'æ',
- "\x1BNKc" => 'ç',
- "\x1BNAe" => 'è',
- "\x1BNBe" => 'é',
- "\x1BNCe" => 'ê',
- "\x1BNHe" => 'ë',
- "\x1BNAi" => 'ì',
- "\x1BNBi" => 'í',
- "\x1BNCi" => 'î',
- "\x1BNHi" => 'ï',
- "\x1BNs" => 'ð',
- "\x1BNDn" => 'ñ',
- "\x1BNAo" => 'ò',
- "\x1BNBo" => 'ó',
- "\x1BNCo" => 'ô',
- "\x1BNDo" => 'õ',
- "\x1BNHo" => 'ö',
- "\x1B/7" => '÷',
- "\x1BNy" => 'ø',
- "\x1BNAu" => 'ù',
- "\x1BNBu" => 'ú',
- "\x1BNCu" => 'û',
- "\x1BNHu" => 'ü',
- "\x1B/=" => 'ý',
- "\x1BN|" => 'þ',
- "\x1BNHy" => 'ÿ',
- );
- }
-
- public static function getIsMbstringEnabled()
- {
- if (isset(self::$isMbstringEnabled)) {
- return self::$isMbstringEnabled;
- }
- self::$isMbstringEnabled = function_exists('mb_convert_encoding') ?
- true : false;
- return self::$isMbstringEnabled;
- }
-
- public static function getIsIconvEnabled()
- {
- if (isset(self::$isIconvEnabled)) {
- return self::$isIconvEnabled;
- }
-
- if (!function_exists('iconv')) {
- self::$isIconvEnabled = false;
- return false;
- }
-
- if (!@iconv('UTF-8', 'UTF-16LE', 'x')) {
- self::$isIconvEnabled = false;
- return false;
- }
-
-
- if (!@iconv_substr('A', 0, 1, 'UTF-8')) {
- self::$isIconvEnabled = false;
- return false;
- }
-
- if (defined('PHP_OS') && @stristr(PHP_OS, 'AIX') && defined('ICONV_IMPL') && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && defined('ICONV_VERSION') && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
- self::$isIconvEnabled = false;
- return false;
- }
-
- self::$isIconvEnabled = true;
- return true;
- }
- public static function buildCharacterSets()
- {
- if (empty(self::$controlCharacters)) {
- self::buildControlCharacters();
- }
- if (empty(self::$SYLKCharacters)) {
- self::buildSYLKCharacters();
- }
- }
-
- public static function ControlCharacterOOXML2PHP($value = '')
- {
- return str_replace(array_keys(self::$controlCharacters), array_values(self::$controlCharacters), $value);
- }
-
- public static function ControlCharacterPHP2OOXML($value = '')
- {
- return str_replace(array_values(self::$controlCharacters), array_keys(self::$controlCharacters), $value);
- }
-
- public static function SanitizeUTF8($value)
- {
- if (self::getIsIconvEnabled()) {
- $value = @iconv('UTF-8', 'UTF-8', $value);
- return $value;
- }
- if (self::getIsMbstringEnabled()) {
- $value = mb_convert_encoding($value, 'UTF-8', 'UTF-8');
- return $value;
- }
-
- return $value;
- }
-
- public static function IsUTF8($value = '')
- {
- return $value === '' || preg_match('/^./su', $value) === 1;
- }
-
- public static function FormatNumber($value)
- {
- if (is_float($value)) {
- return str_replace(',', '.', $value);
- }
- return (string) $value;
- }
-
- public static function UTF8toBIFF8UnicodeShort($value, $arrcRuns = array())
- {
-
- $ln = self::CountCharacters($value, 'UTF-8');
-
- if (empty($arrcRuns)) {
- $opt = (self::getIsIconvEnabled() || self::getIsMbstringEnabled()) ?
- 0x0001 : 0x0000;
- $data = pack('CC', $ln, $opt);
-
- $data .= self::ConvertEncoding($value, 'UTF-16LE', 'UTF-8');
- } else {
- $data = pack('vC', $ln, 0x09);
- $data .= pack('v', count($arrcRuns));
-
- $data .= self::ConvertEncoding($value, 'UTF-16LE', 'UTF-8');
- foreach ($arrcRuns as $cRun) {
- $data .= pack('v', $cRun['strlen']);
- $data .= pack('v', $cRun['fontidx']);
- }
- }
- return $data;
- }
-
- public static function UTF8toBIFF8UnicodeLong($value)
- {
-
- $ln = self::CountCharacters($value, 'UTF-8');
-
- $opt = (self::getIsIconvEnabled() || self::getIsMbstringEnabled()) ?
- 0x0001 : 0x0000;
-
- $chars = self::ConvertEncoding($value, 'UTF-16LE', 'UTF-8');
- $data = pack('vC', $ln, $opt) . $chars;
- return $data;
- }
-
- public static function ConvertEncoding($value, $to, $from)
- {
- if (self::getIsIconvEnabled()) {
- return iconv($from, $to, $value);
- }
- if (self::getIsMbstringEnabled()) {
- return mb_convert_encoding($value, $to, $from);
- }
- if ($from == 'UTF-16LE') {
- return self::utf16_decode($value, false);
- } elseif ($from == 'UTF-16BE') {
- return self::utf16_decode($value);
- }
-
- return $value;
- }
-
- public static function utf16_decode($str, $bom_be = true)
- {
- if (strlen($str) < 2) {
- return $str;
- }
- $c0 = ord($str{0});
- $c1 = ord($str{1});
- if ($c0 == 0xfe && $c1 == 0xff) {
- $str = substr($str, 2);
- } elseif ($c0 == 0xff && $c1 == 0xfe) {
- $str = substr($str, 2);
- $bom_be = false;
- }
- $len = strlen($str);
- $newstr = '';
- for ($i=0; $i<$len; $i+=2) {
- if ($bom_be) {
- $val = ord($str{$i}) << 4;
- $val += ord($str{$i+1});
- } else {
- $val = ord($str{$i+1}) << 4;
- $val += ord($str{$i});
- }
- $newstr .= ($val == 0x228) ? "\n" : chr($val);
- }
- return $newstr;
- }
-
- public static function CountCharacters($value, $enc = 'UTF-8')
- {
- if (self::getIsMbstringEnabled()) {
- return mb_strlen($value, $enc);
- }
- if (self::getIsIconvEnabled()) {
- return iconv_strlen($value, $enc);
- }
-
- return strlen($value);
- }
-
- public static function Substring($pValue = '', $pStart = 0, $pLength = 0)
- {
- if (self::getIsMbstringEnabled()) {
- return mb_substr($pValue, $pStart, $pLength, 'UTF-8');
- }
- if (self::getIsIconvEnabled()) {
- return iconv_substr($pValue, $pStart, $pLength, 'UTF-8');
- }
-
- return substr($pValue, $pStart, $pLength);
- }
-
- public static function StrToUpper($pValue = '')
- {
- if (function_exists('mb_convert_case')) {
- return mb_convert_case($pValue, MB_CASE_UPPER, "UTF-8");
- }
- return strtoupper($pValue);
- }
-
- public static function StrToLower($pValue = '')
- {
- if (function_exists('mb_convert_case')) {
- return mb_convert_case($pValue, MB_CASE_LOWER, "UTF-8");
- }
- return strtolower($pValue);
- }
-
- public static function StrToTitle($pValue = '')
- {
- if (function_exists('mb_convert_case')) {
- return mb_convert_case($pValue, MB_CASE_TITLE, "UTF-8");
- }
- return ucwords($pValue);
- }
- public static function mb_is_upper($char)
- {
- return mb_strtolower($char, "UTF-8") != $char;
- }
- public static function mb_str_split($string)
- {
-
-
- return preg_split('/(?<!^)(?!$)/u', $string);
- }
-
- public static function StrCaseReverse($pValue = '')
- {
- if (self::getIsMbstringEnabled()) {
- $characters = self::mb_str_split($pValue);
- foreach ($characters as &$character) {
- if (self::mb_is_upper($character)) {
- $character = mb_strtolower($character, 'UTF-8');
- } else {
- $character = mb_strtoupper($character, 'UTF-8');
- }
- }
- return implode('', $characters);
- }
- return strtolower($pValue) ^ strtoupper($pValue) ^ $pValue;
- }
-
- public static function convertToNumberIfFraction(&$operand)
- {
- if (preg_match('/^'.self::STRING_REGEXP_FRACTION.'$/i', $operand, $match)) {
- $sign = ($match[1] == '-') ? '-' : '+';
- $fractionFormula = '='.$sign.$match[2].$sign.$match[3];
- $operand = PHPExcel_Calculation::getInstance()->_calculateFormulaValue($fractionFormula);
- return true;
- }
- return false;
- }
-
- public static function getDecimalSeparator()
- {
- if (!isset(self::$decimalSeparator)) {
- $localeconv = localeconv();
- self::$decimalSeparator = ($localeconv['decimal_point'] != '')
- ? $localeconv['decimal_point'] : $localeconv['mon_decimal_point'];
- if (self::$decimalSeparator == '') {
-
- self::$decimalSeparator = '.';
- }
- }
- return self::$decimalSeparator;
- }
-
- public static function setDecimalSeparator($pValue = '.')
- {
- self::$decimalSeparator = $pValue;
- }
-
- public static function getThousandsSeparator()
- {
- if (!isset(self::$thousandsSeparator)) {
- $localeconv = localeconv();
- self::$thousandsSeparator = ($localeconv['thousands_sep'] != '')
- ? $localeconv['thousands_sep'] : $localeconv['mon_thousands_sep'];
- if (self::$thousandsSeparator == '') {
-
- self::$thousandsSeparator = ',';
- }
- }
- return self::$thousandsSeparator;
- }
-
- public static function setThousandsSeparator($pValue = ',')
- {
- self::$thousandsSeparator = $pValue;
- }
-
- public static function getCurrencyCode()
- {
- if (!isset(self::$currencyCode)) {
- $localeconv = localeconv();
- self::$currencyCode = ($localeconv['currency_symbol'] != '')
- ? $localeconv['currency_symbol'] : $localeconv['int_curr_symbol'];
- if (self::$currencyCode == '') {
-
- self::$currencyCode = '$';
- }
- }
- return self::$currencyCode;
- }
-
- public static function setCurrencyCode($pValue = '$')
- {
- self::$currencyCode = $pValue;
- }
-
- public static function SYLKtoUTF8($pValue = '')
- {
-
- if (strpos($pValue, '') === false) {
- return $pValue;
- }
- foreach (self::$SYLKCharacters as $k => $v) {
- $pValue = str_replace($k, $v, $pValue);
- }
- return $pValue;
- }
-
- public static function testStringAsNumeric($value)
- {
- if (is_numeric($value)) {
- return $value;
- }
- $v = floatval($value);
- return (is_numeric(substr($value, 0, strlen($v)))) ? $v : $value;
- }
- }
|