Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Shared;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
   6  
   7  class StringHelper
   8  {
   9      /**    Constants                */
  10      /**    Regular Expressions        */
  11      //    Fraction
  12      const STRING_REGEXP_FRACTION = '~^\s*(-?)((\d*)\s+)?(\d+\/\d+)\s*$~';
  13  
  14      /**
  15       * Control characters array.
  16       *
  17       * @var string[]
  18       */
  19      private static $controlCharacters = [];
  20  
  21      /**
  22       * SYLK Characters array.
  23       *
  24       * @var array
  25       */
  26      private static $SYLKCharacters = [];
  27  
  28      /**
  29       * Decimal separator.
  30       *
  31       * @var ?string
  32       */
  33      private static $decimalSeparator;
  34  
  35      /**
  36       * Thousands separator.
  37       *
  38       * @var ?string
  39       */
  40      private static $thousandsSeparator;
  41  
  42      /**
  43       * Currency code.
  44       *
  45       * @var string
  46       */
  47      private static $currencyCode;
  48  
  49      /**
  50       * Is iconv extension avalable?
  51       *
  52       * @var ?bool
  53       */
  54      private static $isIconvEnabled;
  55  
  56      /**
  57       * iconv options.
  58       *
  59       * @var string
  60       */
  61      private static $iconvOptions = '//IGNORE//TRANSLIT';
  62  
  63      /**
  64       * Build control characters array.
  65       */
  66      private static function buildControlCharacters(): void
  67      {
  68          for ($i = 0; $i <= 31; ++$i) {
  69              if ($i != 9 && $i != 10 && $i != 13) {
  70                  $find = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_';
  71                  $replace = chr($i);
  72                  self::$controlCharacters[$find] = $replace;
  73              }
  74          }
  75      }
  76  
  77      /**
  78       * Build SYLK characters array.
  79       */
  80      private static function buildSYLKCharacters(): void
  81      {
  82          self::$SYLKCharacters = [
  83              "\x1B 0" => chr(0),
  84              "\x1B 1" => chr(1),
  85              "\x1B 2" => chr(2),
  86              "\x1B 3" => chr(3),
  87              "\x1B 4" => chr(4),
  88              "\x1B 5" => chr(5),
  89              "\x1B 6" => chr(6),
  90              "\x1B 7" => chr(7),
  91              "\x1B 8" => chr(8),
  92              "\x1B 9" => chr(9),
  93              "\x1B :" => chr(10),
  94              "\x1B ;" => chr(11),
  95              "\x1B <" => chr(12),
  96              "\x1B =" => chr(13),
  97              "\x1B >" => chr(14),
  98              "\x1B ?" => chr(15),
  99              "\x1B!0" => chr(16),
 100              "\x1B!1" => chr(17),
 101              "\x1B!2" => chr(18),
 102              "\x1B!3" => chr(19),
 103              "\x1B!4" => chr(20),
 104              "\x1B!5" => chr(21),
 105              "\x1B!6" => chr(22),
 106              "\x1B!7" => chr(23),
 107              "\x1B!8" => chr(24),
 108              "\x1B!9" => chr(25),
 109              "\x1B!:" => chr(26),
 110              "\x1B!;" => chr(27),
 111              "\x1B!<" => chr(28),
 112              "\x1B!=" => chr(29),
 113              "\x1B!>" => chr(30),
 114              "\x1B!?" => chr(31),
 115              "\x1B'?" => chr(127),
 116              "\x1B(0" => '€', // 128 in CP1252
 117              "\x1B(2" => '‚', // 130 in CP1252
 118              "\x1B(3" => 'ƒ', // 131 in CP1252
 119              "\x1B(4" => '„', // 132 in CP1252
 120              "\x1B(5" => '…', // 133 in CP1252
 121              "\x1B(6" => '†', // 134 in CP1252
 122              "\x1B(7" => '‡', // 135 in CP1252
 123              "\x1B(8" => 'ˆ', // 136 in CP1252
 124              "\x1B(9" => '‰', // 137 in CP1252
 125              "\x1B(:" => 'Š', // 138 in CP1252
 126              "\x1B(;" => '‹', // 139 in CP1252
 127              "\x1BNj" => 'Œ', // 140 in CP1252
 128              "\x1B(>" => 'Ž', // 142 in CP1252
 129              "\x1B)1" => '‘', // 145 in CP1252
 130              "\x1B)2" => '’', // 146 in CP1252
 131              "\x1B)3" => '“', // 147 in CP1252
 132              "\x1B)4" => '”', // 148 in CP1252
 133              "\x1B)5" => '•', // 149 in CP1252
 134              "\x1B)6" => '–', // 150 in CP1252
 135              "\x1B)7" => '—', // 151 in CP1252
 136              "\x1B)8" => '˜', // 152 in CP1252
 137              "\x1B)9" => '™', // 153 in CP1252
 138              "\x1B):" => 'š', // 154 in CP1252
 139              "\x1B);" => '›', // 155 in CP1252
 140              "\x1BNz" => 'œ', // 156 in CP1252
 141              "\x1B)>" => 'ž', // 158 in CP1252
 142              "\x1B)?" => 'Ÿ', // 159 in CP1252
 143              "\x1B*0" => ' ', // 160 in CP1252
 144              "\x1BN!" => '¡', // 161 in CP1252
 145              "\x1BN\"" => '¢', // 162 in CP1252
 146              "\x1BN#" => '£', // 163 in CP1252
 147              "\x1BN(" => '¤', // 164 in CP1252
 148              "\x1BN%" => '¥', // 165 in CP1252
 149              "\x1B*6" => '¦', // 166 in CP1252
 150              "\x1BN'" => '§', // 167 in CP1252
 151              "\x1BNH " => '¨', // 168 in CP1252
 152              "\x1BNS" => '©', // 169 in CP1252
 153              "\x1BNc" => 'ª', // 170 in CP1252
 154              "\x1BN+" => '«', // 171 in CP1252
 155              "\x1B*<" => '¬', // 172 in CP1252
 156              "\x1B*=" => '­', // 173 in CP1252
 157              "\x1BNR" => '®', // 174 in CP1252
 158              "\x1B*?" => '¯', // 175 in CP1252
 159              "\x1BN0" => '°', // 176 in CP1252
 160              "\x1BN1" => '±', // 177 in CP1252
 161              "\x1BN2" => '²', // 178 in CP1252
 162              "\x1BN3" => '³', // 179 in CP1252
 163              "\x1BNB " => '´', // 180 in CP1252
 164              "\x1BN5" => 'µ', // 181 in CP1252
 165              "\x1BN6" => '¶', // 182 in CP1252
 166              "\x1BN7" => '·', // 183 in CP1252
 167              "\x1B+8" => '¸', // 184 in CP1252
 168              "\x1BNQ" => '¹', // 185 in CP1252
 169              "\x1BNk" => 'º', // 186 in CP1252
 170              "\x1BN;" => '»', // 187 in CP1252
 171              "\x1BN<" => '¼', // 188 in CP1252
 172              "\x1BN=" => '½', // 189 in CP1252
 173              "\x1BN>" => '¾', // 190 in CP1252
 174              "\x1BN?" => '¿', // 191 in CP1252
 175              "\x1BNAA" => 'À', // 192 in CP1252
 176              "\x1BNBA" => 'Á', // 193 in CP1252
 177              "\x1BNCA" => 'Â', // 194 in CP1252
 178              "\x1BNDA" => 'Ã', // 195 in CP1252
 179              "\x1BNHA" => 'Ä', // 196 in CP1252
 180              "\x1BNJA" => 'Å', // 197 in CP1252
 181              "\x1BNa" => 'Æ', // 198 in CP1252
 182              "\x1BNKC" => 'Ç', // 199 in CP1252
 183              "\x1BNAE" => 'È', // 200 in CP1252
 184              "\x1BNBE" => 'É', // 201 in CP1252
 185              "\x1BNCE" => 'Ê', // 202 in CP1252
 186              "\x1BNHE" => 'Ë', // 203 in CP1252
 187              "\x1BNAI" => 'Ì', // 204 in CP1252
 188              "\x1BNBI" => 'Í', // 205 in CP1252
 189              "\x1BNCI" => 'Î', // 206 in CP1252
 190              "\x1BNHI" => 'Ï', // 207 in CP1252
 191              "\x1BNb" => 'Ð', // 208 in CP1252
 192              "\x1BNDN" => 'Ñ', // 209 in CP1252
 193              "\x1BNAO" => 'Ò', // 210 in CP1252
 194              "\x1BNBO" => 'Ó', // 211 in CP1252
 195              "\x1BNCO" => 'Ô', // 212 in CP1252
 196              "\x1BNDO" => 'Õ', // 213 in CP1252
 197              "\x1BNHO" => 'Ö', // 214 in CP1252
 198              "\x1B-7" => '×', // 215 in CP1252
 199              "\x1BNi" => 'Ø', // 216 in CP1252
 200              "\x1BNAU" => 'Ù', // 217 in CP1252
 201              "\x1BNBU" => 'Ú', // 218 in CP1252
 202              "\x1BNCU" => 'Û', // 219 in CP1252
 203              "\x1BNHU" => 'Ü', // 220 in CP1252
 204              "\x1B-=" => 'Ý', // 221 in CP1252
 205              "\x1BNl" => 'Þ', // 222 in CP1252
 206              "\x1BN{" => 'ß', // 223 in CP1252
 207              "\x1BNAa" => 'à', // 224 in CP1252
 208              "\x1BNBa" => 'á', // 225 in CP1252
 209              "\x1BNCa" => 'â', // 226 in CP1252
 210              "\x1BNDa" => 'ã', // 227 in CP1252
 211              "\x1BNHa" => 'ä', // 228 in CP1252
 212              "\x1BNJa" => 'å', // 229 in CP1252
 213              "\x1BNq" => 'æ', // 230 in CP1252
 214              "\x1BNKc" => 'ç', // 231 in CP1252
 215              "\x1BNAe" => 'è', // 232 in CP1252
 216              "\x1BNBe" => 'é', // 233 in CP1252
 217              "\x1BNCe" => 'ê', // 234 in CP1252
 218              "\x1BNHe" => 'ë', // 235 in CP1252
 219              "\x1BNAi" => 'ì', // 236 in CP1252
 220              "\x1BNBi" => 'í', // 237 in CP1252
 221              "\x1BNCi" => 'î', // 238 in CP1252
 222              "\x1BNHi" => 'ï', // 239 in CP1252
 223              "\x1BNs" => 'ð', // 240 in CP1252
 224              "\x1BNDn" => 'ñ', // 241 in CP1252
 225              "\x1BNAo" => 'ò', // 242 in CP1252
 226              "\x1BNBo" => 'ó', // 243 in CP1252
 227              "\x1BNCo" => 'ô', // 244 in CP1252
 228              "\x1BNDo" => 'õ', // 245 in CP1252
 229              "\x1BNHo" => 'ö', // 246 in CP1252
 230              "\x1B/7" => '÷', // 247 in CP1252
 231              "\x1BNy" => 'ø', // 248 in CP1252
 232              "\x1BNAu" => 'ù', // 249 in CP1252
 233              "\x1BNBu" => 'ú', // 250 in CP1252
 234              "\x1BNCu" => 'û', // 251 in CP1252
 235              "\x1BNHu" => 'ü', // 252 in CP1252
 236              "\x1B/=" => 'ý', // 253 in CP1252
 237              "\x1BN|" => 'þ', // 254 in CP1252
 238              "\x1BNHy" => 'ÿ', // 255 in CP1252
 239          ];
 240      }
 241  
 242      /**
 243       * Get whether iconv extension is available.
 244       *
 245       * @return bool
 246       */
 247      public static function getIsIconvEnabled()
 248      {
 249          if (isset(self::$isIconvEnabled)) {
 250              return self::$isIconvEnabled;
 251          }
 252  
 253          // Assume no problems with iconv
 254          self::$isIconvEnabled = true;
 255  
 256          // Fail if iconv doesn't exist
 257          if (!function_exists('iconv')) {
 258              self::$isIconvEnabled = false;
 259          } elseif (!@iconv('UTF-8', 'UTF-16LE', 'x')) {
 260              // Sometimes iconv is not working, and e.g. iconv('UTF-8', 'UTF-16LE', 'x') just returns false,
 261              self::$isIconvEnabled = false;
 262          } elseif (defined('PHP_OS') && @stristr(PHP_OS, 'AIX') && defined('ICONV_IMPL') && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && defined('ICONV_VERSION') && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
 263              // CUSTOM: IBM AIX iconv() does not work
 264              self::$isIconvEnabled = false;
 265          }
 266  
 267          // Deactivate iconv default options if they fail (as seen on IMB i)
 268          if (self::$isIconvEnabled && !@iconv('UTF-8', 'UTF-16LE' . self::$iconvOptions, 'x')) {
 269              self::$iconvOptions = '';
 270          }
 271  
 272          return self::$isIconvEnabled;
 273      }
 274  
 275      private static function buildCharacterSets(): void
 276      {
 277          if (empty(self::$controlCharacters)) {
 278              self::buildControlCharacters();
 279          }
 280  
 281          if (empty(self::$SYLKCharacters)) {
 282              self::buildSYLKCharacters();
 283          }
 284      }
 285  
 286      /**
 287       * Convert from OpenXML escaped control character to PHP control character.
 288       *
 289       * Excel 2007 team:
 290       * ----------------
 291       * That's correct, control characters are stored directly in the shared-strings table.
 292       * We do encode characters that cannot be represented in XML using the following escape sequence:
 293       * _xHHHH_ where H represents a hexadecimal character in the character's value...
 294       * So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
 295       * element or in the shared string <t> element.
 296       *
 297       * @param string $textValue Value to unescape
 298       *
 299       * @return string
 300       */
 301      public static function controlCharacterOOXML2PHP($textValue)
 302      {
 303          self::buildCharacterSets();
 304  
 305          return str_replace(array_keys(self::$controlCharacters), array_values(self::$controlCharacters), $textValue);
 306      }
 307  
 308      /**
 309       * Convert from PHP control character to OpenXML escaped control character.
 310       *
 311       * Excel 2007 team:
 312       * ----------------
 313       * That's correct, control characters are stored directly in the shared-strings table.
 314       * We do encode characters that cannot be represented in XML using the following escape sequence:
 315       * _xHHHH_ where H represents a hexadecimal character in the character's value...
 316       * So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
 317       * element or in the shared string <t> element.
 318       *
 319       * @param string $textValue Value to escape
 320       *
 321       * @return string
 322       */
 323      public static function controlCharacterPHP2OOXML($textValue)
 324      {
 325          self::buildCharacterSets();
 326  
 327          return str_replace(array_values(self::$controlCharacters), array_keys(self::$controlCharacters), $textValue);
 328      }
 329  
 330      /**
 331       * Try to sanitize UTF8, replacing invalid sequences with Unicode substitution characters.
 332       */
 333      public static function sanitizeUTF8(string $textValue): string
 334      {
 335          $textValue = str_replace(["\xef\xbf\xbe", "\xef\xbf\xbf"], "\xef\xbf\xbd", $textValue);
 336          $subst = mb_substitute_character(); // default is question mark
 337          mb_substitute_character(65533); // Unicode substitution character
 338          // Phpstan does not think this can return false.
 339          $returnValue = mb_convert_encoding($textValue, 'UTF-8', 'UTF-8');
 340          mb_substitute_character(/** @scrutinizer ignore-type */ $subst);
 341  
 342          return self::returnString($returnValue);
 343      }
 344  
 345      /**
 346       * Strictly to satisfy Scrutinizer.
 347       *
 348       * @param mixed $value
 349       */
 350      private static function returnString($value): string
 351      {
 352          return is_string($value) ? $value : '';
 353      }
 354  
 355      /**
 356       * Check if a string contains UTF8 data.
 357       */
 358      public static function isUTF8(string $textValue): bool
 359      {
 360          return $textValue === self::sanitizeUTF8($textValue);
 361      }
 362  
 363      /**
 364       * Formats a numeric value as a string for output in various output writers forcing
 365       * point as decimal separator in case locale is other than English.
 366       *
 367       * @param float|int|string $numericValue
 368       */
 369      public static function formatNumber($numericValue): string
 370      {
 371          if (is_float($numericValue)) {
 372              return str_replace(',', '.', (string) $numericValue);
 373          }
 374  
 375          return (string) $numericValue;
 376      }
 377  
 378      /**
 379       * Converts a UTF-8 string into BIFF8 Unicode string data (8-bit string length)
 380       * Writes the string using uncompressed notation, no rich text, no Asian phonetics
 381       * If mbstring extension is not available, ASCII is assumed, and compressed notation is used
 382       * although this will give wrong results for non-ASCII strings
 383       * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3.
 384       *
 385       * @param string $textValue UTF-8 encoded string
 386       * @param mixed[] $arrcRuns Details of rich text runs in $value
 387       */
 388      public static function UTF8toBIFF8UnicodeShort(string $textValue, array $arrcRuns = []): string
 389      {
 390          // character count
 391          $ln = self::countCharacters($textValue, 'UTF-8');
 392          // option flags
 393          if (empty($arrcRuns)) {
 394              $data = pack('CC', $ln, 0x0001);
 395              // characters
 396              $data .= self::convertEncoding($textValue, 'UTF-16LE', 'UTF-8');
 397          } else {
 398              $data = pack('vC', $ln, 0x09);
 399              $data .= pack('v', count($arrcRuns));
 400              // characters
 401              $data .= self::convertEncoding($textValue, 'UTF-16LE', 'UTF-8');
 402              foreach ($arrcRuns as $cRun) {
 403                  $data .= pack('v', $cRun['strlen']);
 404                  $data .= pack('v', $cRun['fontidx']);
 405              }
 406          }
 407  
 408          return $data;
 409      }
 410  
 411      /**
 412       * Converts a UTF-8 string into BIFF8 Unicode string data (16-bit string length)
 413       * Writes the string using uncompressed notation, no rich text, no Asian phonetics
 414       * If mbstring extension is not available, ASCII is assumed, and compressed notation is used
 415       * although this will give wrong results for non-ASCII strings
 416       * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3.
 417       *
 418       * @param string $textValue UTF-8 encoded string
 419       */
 420      public static function UTF8toBIFF8UnicodeLong(string $textValue): string
 421      {
 422          // character count
 423          $ln = self::countCharacters($textValue, 'UTF-8');
 424  
 425          // characters
 426          $chars = self::convertEncoding($textValue, 'UTF-16LE', 'UTF-8');
 427  
 428          return pack('vC', $ln, 0x0001) . $chars;
 429      }
 430  
 431      /**
 432       * Convert string from one encoding to another.
 433       *
 434       * @param string $to Encoding to convert to, e.g. 'UTF-8'
 435       * @param string $from Encoding to convert from, e.g. 'UTF-16LE'
 436       */
 437      public static function convertEncoding(string $textValue, string $to, string $from): string
 438      {
 439          if (self::getIsIconvEnabled()) {
 440              $result = iconv($from, $to . self::$iconvOptions, $textValue);
 441              if (false !== $result) {
 442                  return $result;
 443              }
 444          }
 445  
 446          return self::returnString(mb_convert_encoding($textValue, $to, $from));
 447      }
 448  
 449      /**
 450       * Get character count.
 451       *
 452       * @param string $encoding Encoding
 453       *
 454       * @return int Character count
 455       */
 456      public static function countCharacters(string $textValue, string $encoding = 'UTF-8'): int
 457      {
 458          return mb_strlen($textValue, $encoding);
 459      }
 460  
 461      /**
 462       * Get a substring of a UTF-8 encoded string.
 463       *
 464       * @param string $textValue UTF-8 encoded string
 465       * @param int $offset Start offset
 466       * @param ?int $length Maximum number of characters in substring
 467       */
 468      public static function substring(string $textValue, int $offset, ?int $length = 0): string
 469      {
 470          return mb_substr($textValue, $offset, $length, 'UTF-8');
 471      }
 472  
 473      /**
 474       * Convert a UTF-8 encoded string to upper case.
 475       *
 476       * @param string $textValue UTF-8 encoded string
 477       */
 478      public static function strToUpper(string $textValue): string
 479      {
 480          return mb_convert_case($textValue, MB_CASE_UPPER, 'UTF-8');
 481      }
 482  
 483      /**
 484       * Convert a UTF-8 encoded string to lower case.
 485       *
 486       * @param string $textValue UTF-8 encoded string
 487       */
 488      public static function strToLower(string $textValue): string
 489      {
 490          return mb_convert_case($textValue, MB_CASE_LOWER, 'UTF-8');
 491      }
 492  
 493      /**
 494       * Convert a UTF-8 encoded string to title/proper case
 495       * (uppercase every first character in each word, lower case all other characters).
 496       *
 497       * @param string $textValue UTF-8 encoded string
 498       */
 499      public static function strToTitle(string $textValue): string
 500      {
 501          return mb_convert_case($textValue, MB_CASE_TITLE, 'UTF-8');
 502      }
 503  
 504      public static function mbIsUpper(string $character): bool
 505      {
 506          return mb_strtolower($character, 'UTF-8') !== $character;
 507      }
 508  
 509      /**
 510       * Splits a UTF-8 string into an array of individual characters.
 511       */
 512      public static function mbStrSplit(string $string): array
 513      {
 514          // Split at all position not after the start: ^
 515          // and not before the end: $
 516          $split = preg_split('/(?<!^)(?!$)/u', $string);
 517  
 518          return ($split === false) ? [] : $split;
 519      }
 520  
 521      /**
 522       * Reverse the case of a string, so that all uppercase characters become lowercase
 523       * and all lowercase characters become uppercase.
 524       *
 525       * @param string $textValue UTF-8 encoded string
 526       */
 527      public static function strCaseReverse(string $textValue): string
 528      {
 529          $characters = self::mbStrSplit($textValue);
 530          foreach ($characters as &$character) {
 531              if (self::mbIsUpper($character)) {
 532                  $character = mb_strtolower($character, 'UTF-8');
 533              } else {
 534                  $character = mb_strtoupper($character, 'UTF-8');
 535              }
 536          }
 537  
 538          return implode('', $characters);
 539      }
 540  
 541      /**
 542       * Identify whether a string contains a fractional numeric value,
 543       * and convert it to a numeric if it is.
 544       *
 545       * @param string $operand string value to test
 546       */
 547      public static function convertToNumberIfFraction(string &$operand): bool
 548      {
 549          if (preg_match(self::STRING_REGEXP_FRACTION, $operand, $match)) {
 550              $sign = ($match[1] == '-') ? '-' : '+';
 551              $wholePart = ($match[3] === '') ? '' : ($sign . $match[3]);
 552              $fractionFormula = '=' . $wholePart . $sign . $match[4];
 553              $operand = Calculation::getInstance()->_calculateFormulaValue($fractionFormula);
 554  
 555              return true;
 556          }
 557  
 558          return false;
 559      }
 560  
 561      //    function convertToNumberIfFraction()
 562  
 563      /**
 564       * Get the decimal separator. If it has not yet been set explicitly, try to obtain number
 565       * formatting information from locale.
 566       */
 567      public static function getDecimalSeparator(): string
 568      {
 569          if (!isset(self::$decimalSeparator)) {
 570              $localeconv = localeconv();
 571              self::$decimalSeparator = ($localeconv['decimal_point'] != '')
 572                  ? $localeconv['decimal_point'] : $localeconv['mon_decimal_point'];
 573  
 574              if (self::$decimalSeparator == '') {
 575                  // Default to .
 576                  self::$decimalSeparator = '.';
 577              }
 578          }
 579  
 580          return self::$decimalSeparator;
 581      }
 582  
 583      /**
 584       * Set the decimal separator. Only used by NumberFormat::toFormattedString()
 585       * to format output by \PhpOffice\PhpSpreadsheet\Writer\Html and \PhpOffice\PhpSpreadsheet\Writer\Pdf.
 586       *
 587       * @param string $separator Character for decimal separator
 588       */
 589      public static function setDecimalSeparator(string $separator): void
 590      {
 591          self::$decimalSeparator = $separator;
 592      }
 593  
 594      /**
 595       * Get the thousands separator. If it has not yet been set explicitly, try to obtain number
 596       * formatting information from locale.
 597       */
 598      public static function getThousandsSeparator(): string
 599      {
 600          if (!isset(self::$thousandsSeparator)) {
 601              $localeconv = localeconv();
 602              self::$thousandsSeparator = ($localeconv['thousands_sep'] != '')
 603                  ? $localeconv['thousands_sep'] : $localeconv['mon_thousands_sep'];
 604  
 605              if (self::$thousandsSeparator == '') {
 606                  // Default to .
 607                  self::$thousandsSeparator = ',';
 608              }
 609          }
 610  
 611          return self::$thousandsSeparator;
 612      }
 613  
 614      /**
 615       * Set the thousands separator. Only used by NumberFormat::toFormattedString()
 616       * to format output by \PhpOffice\PhpSpreadsheet\Writer\Html and \PhpOffice\PhpSpreadsheet\Writer\Pdf.
 617       *
 618       * @param string $separator Character for thousands separator
 619       */
 620      public static function setThousandsSeparator(string $separator): void
 621      {
 622          self::$thousandsSeparator = $separator;
 623      }
 624  
 625      /**
 626       *    Get the currency code. If it has not yet been set explicitly, try to obtain the
 627       *        symbol information from locale.
 628       */
 629      public static function getCurrencyCode(): string
 630      {
 631          if (!empty(self::$currencyCode)) {
 632              return self::$currencyCode;
 633          }
 634          self::$currencyCode = '$';
 635          $localeconv = localeconv();
 636          if (!empty($localeconv['currency_symbol'])) {
 637              self::$currencyCode = $localeconv['currency_symbol'];
 638  
 639              return self::$currencyCode;
 640          }
 641          if (!empty($localeconv['int_curr_symbol'])) {
 642              self::$currencyCode = $localeconv['int_curr_symbol'];
 643  
 644              return self::$currencyCode;
 645          }
 646  
 647          return self::$currencyCode;
 648      }
 649  
 650      /**
 651       * Set the currency code. Only used by NumberFormat::toFormattedString()
 652       *        to format output by \PhpOffice\PhpSpreadsheet\Writer\Html and \PhpOffice\PhpSpreadsheet\Writer\Pdf.
 653       *
 654       * @param string $currencyCode Character for currency code
 655       */
 656      public static function setCurrencyCode(string $currencyCode): void
 657      {
 658          self::$currencyCode = $currencyCode;
 659      }
 660  
 661      /**
 662       * Convert SYLK encoded string to UTF-8.
 663       *
 664       * @param string $textValue SYLK encoded string
 665       *
 666       * @return string UTF-8 encoded string
 667       */
 668      public static function SYLKtoUTF8(string $textValue): string
 669      {
 670          self::buildCharacterSets();
 671  
 672          // If there is no escape character in the string there is nothing to do
 673          if (strpos($textValue, '') === false) {
 674              return $textValue;
 675          }
 676  
 677          foreach (self::$SYLKCharacters as $k => $v) {
 678              $textValue = str_replace($k, $v, $textValue);
 679          }
 680  
 681          return $textValue;
 682      }
 683  
 684      /**
 685       * Retrieve any leading numeric part of a string, or return the full string if no leading numeric
 686       * (handles basic integer or float, but not exponent or non decimal).
 687       *
 688       * @param string $textValue
 689       *
 690       * @return mixed string or only the leading numeric part of the string
 691       */
 692      public static function testStringAsNumeric($textValue)
 693      {
 694          if (is_numeric($textValue)) {
 695              return $textValue;
 696          }
 697          $v = (float) $textValue;
 698  
 699          return (is_numeric(substr($textValue, 0, strlen((string) $v)))) ? $v : $textValue;
 700      }
 701  }