StringHelper.php 23 KB

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