diff --git a/composer.json b/composer.json index ca41778b..a2778cfb 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ "require": { "php": "^5.5|^7.0", "ext-mbstring": "*", + "ext-iconv": "*", "ext-xml": "*", "ext-xmlwriter": "*" }, diff --git a/src/Autoloader.php b/src/Autoloader.php index 3ed00569..7bca2ef9 100644 --- a/src/Autoloader.php +++ b/src/Autoloader.php @@ -38,7 +38,7 @@ class Autoloader spl_autoload_register('__autoload'); } // Register ourselves with SPL - return spl_autoload_register([\PhpSpreadsheet\Autoloader::class, 'load']); + return spl_autoload_register([\PhpSpreadsheet\Autoloader::class, 'load'], true, true); } /** diff --git a/src/PhpSpreadsheet/Calculation/TextData.php b/src/PhpSpreadsheet/Calculation/TextData.php index 9aa7f13d..8a32a19f 100644 --- a/src/PhpSpreadsheet/Calculation/TextData.php +++ b/src/PhpSpreadsheet/Calculation/TextData.php @@ -28,26 +28,9 @@ class TextData { private static $invalidChars; - private static function unicodeToOrd($c) + private static function unicodeToOrd($character) { - if (ord($c{0}) >= 0 && ord($c{0}) <= 127) { - return ord($c{0}); - } elseif (ord($c{0}) >= 192 && ord($c{0}) <= 223) { - return (ord($c{0}) - 192) * 64 + (ord($c{1}) - 128); - } elseif (ord($c{0}) >= 224 && ord($c{0}) <= 239) { - return (ord($c{0}) - 224) * 4096 + (ord($c{1}) - 128) * 64 + (ord($c{2}) - 128); - } elseif (ord($c{0}) >= 240 && ord($c{0}) <= 247) { - return (ord($c{0}) - 240) * 262144 + (ord($c{1}) - 128) * 4096 + (ord($c{2}) - 128) * 64 + (ord($c{3}) - 128); - } elseif (ord($c{0}) >= 248 && ord($c{0}) <= 251) { - return (ord($c{0}) - 248) * 16777216 + (ord($c{1}) - 128) * 262144 + (ord($c{2}) - 128) * 4096 + (ord($c{3}) - 128) * 64 + (ord($c{4}) - 128); - } elseif (ord($c{0}) >= 252 && ord($c{0}) <= 253) { - return (ord($c{0}) - 252) * 1073741824 + (ord($c{1}) - 128) * 16777216 + (ord($c{2}) - 128) * 262144 + (ord($c{3}) - 128) * 4096 + (ord($c{4}) - 128) * 64 + (ord($c{5}) - 128); - } elseif (ord($c{0}) >= 254 && ord($c{0}) <= 255) { - // error - return Functions::VALUE(); - } - - return 0; + return unpack('V', iconv('UTF-8', 'UCS-4LE', $character))[1]; } /** @@ -64,11 +47,11 @@ class TextData return Functions::VALUE(); } - if (function_exists('mb_convert_encoding')) { - return mb_convert_encoding('&#' . intval($character) . ';', 'UTF-8', 'HTML-ENTITIES'); - } else { - return chr(intval($character)); + if (function_exists('iconv')) { + return iconv('UCS-4LE', 'UTF-8', pack('V', $character)); } + + return mb_convert_encoding('&#' . intval($character) . ';', 'UTF-8', 'HTML-ENTITIES'); } /** diff --git a/tests/data/Calculation/TextData/CHAR.php b/tests/data/Calculation/TextData/CHAR.php index 3915d3db..c6b59428 100644 --- a/tests/data/Calculation/TextData/CHAR.php +++ b/tests/data/Calculation/TextData/CHAR.php @@ -1,24 +1,15 @@