StringHelper.php 23 KB

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