PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/common/libraries/plugin/phpexcel/PHPExcel/Shared/String.php

https://bitbucket.org/renaatdemuynck/chamilo
PHP | 724 lines | 407 code | 58 blank | 259 comment | 62 complexity | cb279fd25991bf4ade7459b1349c0884 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT, GPL-2.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Shared
  23. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. /**
  28. * PHPExcel_Shared_String
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Shared
  32. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Shared_String
  35. {
  36. /** Constants */
  37. /** Regular Expressions */
  38. // Fraction
  39. const STRING_REGEXP_FRACTION = '(-?)(\d+)\s+(\d+\/\d+)';
  40. /**
  41. * Control characters array
  42. *
  43. * @var string[]
  44. */
  45. private static $_controlCharacters = array();
  46. /**
  47. * SYLK Characters array
  48. *
  49. * $var array
  50. */
  51. private static $_SYLKCharacters = array();
  52. /**
  53. * Decimal separator
  54. *
  55. * @var string
  56. */
  57. private static $_decimalSeparator;
  58. /**
  59. * Thousands separator
  60. *
  61. * @var string
  62. */
  63. private static $_thousandsSeparator;
  64. /**
  65. * Currency code
  66. *
  67. * @var string
  68. */
  69. private static $_currencyCode;
  70. /**
  71. * Is mbstring extension avalable?
  72. *
  73. * @var boolean
  74. */
  75. private static $_isMbstringEnabled;
  76. /**
  77. * Is iconv extension avalable?
  78. *
  79. * @var boolean
  80. */
  81. private static $_isIconvEnabled;
  82. /**
  83. * Build control characters array
  84. */
  85. private static function _buildControlCharacters()
  86. {
  87. for($i = 0; $i <= 31; ++ $i)
  88. {
  89. if ($i != 9 && $i != 10 && $i != 13)
  90. {
  91. $find = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_';
  92. $replace = chr($i);
  93. self :: $_controlCharacters[$find] = $replace;
  94. }
  95. }
  96. }
  97. /**
  98. * Build SYLK characters array
  99. */
  100. private static function _buildSYLKCharacters()
  101. {
  102. self :: $_SYLKCharacters = array("\x1B 0" => chr(0), "\x1B 1" => chr(1), "\x1B 2" => chr(2),
  103. "\x1B 3" => chr(3), "\x1B 4" => chr(4), "\x1B 5" => chr(5), "\x1B 6" => chr(6), "\x1B 7" => chr(7),
  104. "\x1B 8" => chr(8), "\x1B 9" => chr(9), "\x1B :" => chr(10), "\x1B ;" => chr(11), "\x1B <" => chr(12),
  105. "\x1B :" => chr(13), "\x1B >" => chr(14), "\x1B ?" => chr(15), "\x1B!0" => chr(16), "\x1B!1" => chr(17),
  106. "\x1B!2" => chr(18), "\x1B!3" => chr(19), "\x1B!4" => chr(20), "\x1B!5" => chr(21), "\x1B!6" => chr(22),
  107. "\x1B!7" => chr(23), "\x1B!8" => chr(24), "\x1B!9" => chr(25), "\x1B!:" => chr(26), "\x1B!;" => chr(27),
  108. "\x1B!<" => chr(28), "\x1B!=" => chr(29), "\x1B!>" => chr(30), "\x1B!?" => chr(31),
  109. "\x1B'?" => chr(127), "\x1B(0" => '€', // 128 in CP1252
  110. "\x1B(2" => '‚', // 130 in CP1252
  111. "\x1B(3" => 'ƒ', // 131 in CP1252
  112. "\x1B(4" => '„', // 132 in CP1252
  113. "\x1B(5" => '…', // 133 in CP1252
  114. "\x1B(6" => '†', // 134 in CP1252
  115. "\x1B(7" => '‡', // 135 in CP1252
  116. "\x1B(8" => 'ˆ', // 136 in CP1252
  117. "\x1B(9" => '‰', // 137 in CP1252
  118. "\x1B(:" => 'Ĺ ', // 138 in CP1252
  119. "\x1B(;" => '‹', // 139 in CP1252
  120. "\x1BNj" => 'Œ', // 140 in CP1252
  121. "\x1B(>" => 'Ĺ˝', // 142 in CP1252
  122. "\x1B)1" => '‘', // 145 in CP1252
  123. "\x1B)2" => '’', // 146 in CP1252
  124. "\x1B)3" => '“', // 147 in CP1252
  125. "\x1B)4" => '”', // 148 in CP1252
  126. "\x1B)5" => '•', // 149 in CP1252
  127. "\x1B)6" => '–', // 150 in CP1252
  128. "\x1B)7" => '—', // 151 in CP1252
  129. "\x1B)8" => '˜', // 152 in CP1252
  130. "\x1B)9" => '™', // 153 in CP1252
  131. "\x1B):" => 'ĹĄ', // 154 in CP1252
  132. "\x1B);" => '›', // 155 in CP1252
  133. "\x1BNz" => 'œ', // 156 in CP1252
  134. "\x1B)>" => 'Ĺž', // 158 in CP1252
  135. "\x1B)?" => 'Ÿ', // 159 in CP1252
  136. "\x1B*0" => ' ', // 160 in CP1252
  137. "\x1BN!" => 'ÂĄ', // 161 in CP1252
  138. "\x1BN\"" => '¢', // 162 in CP1252
  139. "\x1BN#" => 'ÂŁ', // 163 in CP1252
  140. "\x1BN(" => '¤', // 164 in CP1252
  141. "\x1BN%" => 'ÂĽ', // 165 in CP1252
  142. "\x1B*6" => 'ÂŚ', // 166 in CP1252
  143. "\x1BN'" => '§', // 167 in CP1252
  144. "\x1BNH " => '¨', // 168 in CP1252
  145. "\x1BNS" => 'Š', // 169 in CP1252
  146. "\x1BNc" => 'ÂŞ', // 170 in CP1252
  147. "\x1BN+" => 'ÂŤ', // 171 in CP1252
  148. "\x1B*<" => 'ÂŹ', // 172 in CP1252
  149. "\x1B*=" => '­', // 173 in CP1252
  150. "\x1BNR" => 'ÂŽ', // 174 in CP1252
  151. "\x1B*?" => 'ÂŻ', // 175 in CP1252
  152. "\x1BN0" => '°', // 176 in CP1252
  153. "\x1BN1" => 'Âą', // 177 in CP1252
  154. "\x1BN2" => '²', // 178 in CP1252
  155. "\x1BN3" => 'Âł', // 179 in CP1252
  156. "\x1BNB " => '´', // 180 in CP1252
  157. "\x1BN5" => 'Âľ', // 181 in CP1252
  158. "\x1BN6" => 'Âś', // 182 in CP1252
  159. "\x1BN7" => '¡', // 183 in CP1252
  160. "\x1B+8" => '¸', // 184 in CP1252
  161. "\x1BNQ" => 'š', // 185 in CP1252
  162. "\x1BNk" => 'Âş', // 186 in CP1252
  163. "\x1BN;" => 'Âť', // 187 in CP1252
  164. "\x1BN<" => 'Âź', // 188 in CP1252
  165. "\x1BN=" => '½', // 189 in CP1252
  166. "\x1BN>" => 'ž', // 190 in CP1252
  167. "\x1BN?" => 'Âż', // 191 in CP1252
  168. "\x1BNAA" => 'À', // 192 in CP1252
  169. "\x1BNBA" => 'Á', // 193 in CP1252
  170. "\x1BNCA" => 'Â', // 194 in CP1252
  171. "\x1BNDA" => 'Ã', // 195 in CP1252
  172. "\x1BNHA" => 'Ä', // 196 in CP1252
  173. "\x1BNJA" => 'Å', // 197 in CP1252
  174. "\x1BNa" => 'Æ', // 198 in CP1252
  175. "\x1BNKC" => 'Ç', // 199 in CP1252
  176. "\x1BNAE" => 'È', // 200 in CP1252
  177. "\x1BNBE" => 'É', // 201 in CP1252
  178. "\x1BNCE" => 'Ê', // 202 in CP1252
  179. "\x1BNHE" => 'Ë', // 203 in CP1252
  180. "\x1BNAI" => 'Ì', // 204 in CP1252
  181. "\x1BNBI" => 'Í', // 205 in CP1252
  182. "\x1BNCI" => 'Î', // 206 in CP1252
  183. "\x1BNHI" => 'Ï', // 207 in CP1252
  184. "\x1BNb" => 'Ð', // 208 in CP1252
  185. "\x1BNDN" => 'Ñ', // 209 in CP1252
  186. "\x1BNAO" => 'Ò', // 210 in CP1252
  187. "\x1BNBO" => 'Ó', // 211 in CP1252
  188. "\x1BNCO" => 'Ô', // 212 in CP1252
  189. "\x1BNDO" => 'Õ', // 213 in CP1252
  190. "\x1BNHO" => 'Ö', // 214 in CP1252
  191. "\x1B-7" => '×', // 215 in CP1252
  192. "\x1BNi" => 'Ø', // 216 in CP1252
  193. "\x1BNAU" => 'Ù', // 217 in CP1252
  194. "\x1BNBU" => 'Ú', // 218 in CP1252
  195. "\x1BNCU" => 'Û', // 219 in CP1252
  196. "\x1BNHU" => 'Ü', // 220 in CP1252
  197. "\x1B-=" => 'Ý', // 221 in CP1252
  198. "\x1BNl" => 'Þ', // 222 in CP1252
  199. "\x1BN{" => 'ß', // 223 in CP1252
  200. "\x1BNAa" => 'Ă ', // 224 in CP1252
  201. "\x1BNBa" => 'ĂĄ', // 225 in CP1252
  202. "\x1BNCa" => 'â', // 226 in CP1252
  203. "\x1BNDa" => 'ĂŁ', // 227 in CP1252
  204. "\x1BNHa" => 'ä', // 228 in CP1252
  205. "\x1BNJa" => 'ĂĽ', // 229 in CP1252
  206. "\x1BNq" => 'ĂŚ', // 230 in CP1252
  207. "\x1BNKc" => 'ç', // 231 in CP1252
  208. "\x1BNAe" => 'è', // 232 in CP1252
  209. "\x1BNBe" => 'ĂŠ', // 233 in CP1252
  210. "\x1BNCe" => 'ĂŞ', // 234 in CP1252
  211. "\x1BNHe" => 'ĂŤ', // 235 in CP1252
  212. "\x1BNAi" => 'ĂŹ', // 236 in CP1252
  213. "\x1BNBi" => 'Ă­', // 237 in CP1252
  214. "\x1BNCi" => 'ĂŽ', // 238 in CP1252
  215. "\x1BNHi" => 'ĂŻ', // 239 in CP1252
  216. "\x1BNs" => 'Ă°', // 240 in CP1252
  217. "\x1BNDn" => 'Ăą', // 241 in CP1252
  218. "\x1BNAo" => 'ò', // 242 in CP1252
  219. "\x1BNBo" => 'Ăł', // 243 in CP1252
  220. "\x1BNCo" => 'Ă´', // 244 in CP1252
  221. "\x1BNDo" => 'Ăľ', // 245 in CP1252
  222. "\x1BNHo" => 'Ăś', // 246 in CP1252
  223. "\x1B/7" => 'á', // 247 in CP1252
  224. "\x1BNy" => 'ø', // 248 in CP1252
  225. "\x1BNAu" => 'Ăš', // 249 in CP1252
  226. "\x1BNBu" => 'Ăş', // 250 in CP1252
  227. "\x1BNCu" => 'Ăť', // 251 in CP1252
  228. "\x1BNHu" => 'Ăź', // 252 in CP1252
  229. "\x1B/=" => 'Ă˝', // 253 in CP1252
  230. "\x1BN|" => 'Ăž', // 254 in CP1252
  231. "\x1BNHy" => 'Ăż')// 255 in CP1252
  232. ;
  233. }
  234. /**
  235. * Get whether mbstring extension is available
  236. *
  237. * @return boolean
  238. */
  239. public static function getIsMbstringEnabled()
  240. {
  241. if (isset(self :: $_isMbstringEnabled))
  242. {
  243. return self :: $_isMbstringEnabled;
  244. }
  245. self :: $_isMbstringEnabled = function_exists('mb_convert_encoding') ? true : false;
  246. return self :: $_isMbstringEnabled;
  247. }
  248. /**
  249. * Get whether iconv extension is available
  250. *
  251. * @return boolean
  252. */
  253. public static function getIsIconvEnabled()
  254. {
  255. if (isset(self :: $_isIconvEnabled))
  256. {
  257. return self :: $_isIconvEnabled;
  258. }
  259. // Fail if iconv doesn't exist
  260. if (! function_exists('iconv'))
  261. {
  262. self :: $_isIconvEnabled = false;
  263. return false;
  264. }
  265. // Sometimes iconv is not working, and e.g. iconv('UTF-8', 'UTF-16LE', 'x') just returns false,
  266. if (! @iconv('UTF-8', 'UTF-16LE', 'x'))
  267. {
  268. self :: $_isIconvEnabled = false;
  269. return false;
  270. }
  271. // Sometimes iconv_substr('A', 0, 1, 'UTF-8') just returns false in PHP 5.2.0
  272. // we cannot use iconv in that case either (http://bugs.php.net/bug.php?id=37773)
  273. if (! @iconv_substr('A', 0, 1, 'UTF-8'))
  274. {
  275. self :: $_isIconvEnabled = false;
  276. return false;
  277. }
  278. // CUSTOM: IBM AIX iconv() does not work
  279. if (defined('PHP_OS') && @stristr(PHP_OS, 'AIX') && defined('ICONV_IMPL') && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && defined('ICONV_VERSION') && (@strcasecmp(ICONV_VERSION, 'unknown') == 0))
  280. {
  281. self :: $_isIconvEnabled = false;
  282. return false;
  283. }
  284. // If we reach here no problems were detected with iconv
  285. self :: $_isIconvEnabled = true;
  286. return true;
  287. }
  288. public static function buildCharacterSets()
  289. {
  290. if (empty(self :: $_controlCharacters))
  291. {
  292. self :: _buildControlCharacters();
  293. }
  294. if (empty(self :: $_SYLKCharacters))
  295. {
  296. self :: _buildSYLKCharacters();
  297. }
  298. }
  299. /**
  300. * Convert from OpenXML escaped control character to PHP control character
  301. *
  302. * Excel 2007 team:
  303. * ----------------
  304. * That's correct, control characters are stored directly in the shared-strings table.
  305. * We do encode characters that cannot be represented in XML using the following escape sequence:
  306. * _xHHHH_ where H represents a hexadecimal character in the character's value...
  307. * So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
  308. * element or in the shared string <t> element.
  309. *
  310. * @param string $value Value to unescape
  311. * @return string
  312. */
  313. public static function ControlCharacterOOXML2PHP($value = '')
  314. {
  315. return str_replace(array_keys(self :: $_controlCharacters), array_values(self :: $_controlCharacters), $value);
  316. }
  317. /**
  318. * Convert from PHP control character to OpenXML escaped control character
  319. *
  320. * Excel 2007 team:
  321. * ----------------
  322. * That's correct, control characters are stored directly in the shared-strings table.
  323. * We do encode characters that cannot be represented in XML using the following escape sequence:
  324. * _xHHHH_ where H represents a hexadecimal character in the character's value...
  325. * So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
  326. * element or in the shared string <t> element.
  327. *
  328. * @param string $value Value to escape
  329. * @return string
  330. */
  331. public static function ControlCharacterPHP2OOXML($value = '')
  332. {
  333. return str_replace(array_values(self :: $_controlCharacters), array_keys(self :: $_controlCharacters), $value);
  334. }
  335. /**
  336. * Try to sanitize UTF8, stripping invalid byte sequences. Not perfect. Does not surrogate characters.
  337. *
  338. * @param string $value
  339. * @return string
  340. */
  341. public static function SanitizeUTF8($value)
  342. {
  343. if (self :: getIsIconvEnabled())
  344. {
  345. $value = @iconv('UTF-8', 'UTF-8', $value);
  346. return $value;
  347. }
  348. if (self :: getIsMbstringEnabled())
  349. {
  350. $value = mb_convert_encoding($value, 'UTF-8', 'UTF-8');
  351. return $value;
  352. }
  353. // else, no conversion
  354. return $value;
  355. }
  356. /**
  357. * Check if a string contains UTF8 data
  358. *
  359. * @param string $value
  360. * @return boolean
  361. */
  362. public static function IsUTF8($value = '')
  363. {
  364. return utf8_encode(utf8_decode($value)) === $value;
  365. }
  366. /**
  367. * Formats a numeric value as a string for output in various output writers forcing
  368. * point as decimal separator in case locale is other than English.
  369. *
  370. * @param mixed $value
  371. * @return string
  372. */
  373. public static function FormatNumber($value)
  374. {
  375. if (is_float($value))
  376. {
  377. return str_replace(',', '.', $value);
  378. }
  379. return (string) $value;
  380. }
  381. /**
  382. * Converts a UTF-8 string into BIFF8 Unicode string data (8-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. * @return string
  390. */
  391. public static function UTF8toBIFF8UnicodeShort($value)
  392. {
  393. // character count
  394. $ln = self :: CountCharacters($value, 'UTF-8');
  395. // option flags
  396. $opt = (self :: getIsIconvEnabled() || self :: getIsMbstringEnabled()) ? 0x0001 : 0x0000;
  397. // characters
  398. $chars = self :: ConvertEncoding($value, 'UTF-16LE', 'UTF-8');
  399. $data = pack('CC', $ln, $opt) . $chars;
  400. return $data;
  401. }
  402. /**
  403. * Converts a UTF-8 string into BIFF8 Unicode string data (16-bit string length)
  404. * Writes the string using uncompressed notation, no rich text, no Asian phonetics
  405. * If mbstring extension is not available, ASCII is assumed, and compressed notation is used
  406. * although this will give wrong results for non-ASCII strings
  407. * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3
  408. *
  409. * @param string $value UTF-8 encoded string
  410. * @return string
  411. */
  412. public static function UTF8toBIFF8UnicodeLong($value)
  413. {
  414. // character count
  415. $ln = self :: CountCharacters($value, 'UTF-8');
  416. // option flags
  417. $opt = (self :: getIsIconvEnabled() || self :: getIsMbstringEnabled()) ? 0x0001 : 0x0000;
  418. // characters
  419. $chars = self :: ConvertEncoding($value, 'UTF-16LE', 'UTF-8');
  420. $data = pack('vC', $ln, $opt) . $chars;
  421. return $data;
  422. }
  423. /**
  424. * Convert string from one encoding to another. First try iconv, then mbstring, or no convertion
  425. *
  426. * @param string $value
  427. * @param string $to Encoding to convert to, e.g. 'UTF-8'
  428. * @param string $from Encoding to convert from, e.g. 'UTF-16LE'
  429. * @return string
  430. */
  431. public static function ConvertEncoding($value, $to, $from)
  432. {
  433. if (self :: getIsIconvEnabled())
  434. {
  435. $value = iconv($from, $to, $value);
  436. return $value;
  437. }
  438. if (self :: getIsMbstringEnabled())
  439. {
  440. $value = mb_convert_encoding($value, $to, $from);
  441. return $value;
  442. }
  443. if ($from == 'UTF-16LE')
  444. {
  445. return self :: utf16_decode($value, false);
  446. }
  447. else
  448. if ($from == 'UTF-16BE')
  449. {
  450. return self :: utf16_decode($value);
  451. }
  452. // else, no conversion
  453. return $value;
  454. }
  455. /**
  456. * Decode UTF-16 encoded strings.
  457. *
  458. * Can handle both BOM'ed data and un-BOM'ed data.
  459. * Assumes Big-Endian byte order if no BOM is available.
  460. * This function was taken from http://php.net/manual/en/function.utf8-decode.php
  461. * and $bom_be parameter added.
  462. *
  463. * @param string $str UTF-16 encoded data to decode.
  464. * @return string UTF-8 / ISO encoded data.
  465. * @access public
  466. * @version 0.2 / 2010-05-13
  467. * @author Rasmus Andersson {@link http://rasmusandersson.se/}
  468. * @author vadik56
  469. */
  470. public static function utf16_decode($str, $bom_be = true)
  471. {
  472. if (strlen($str) < 2)
  473. return $str;
  474. $c0 = ord($str{0});
  475. $c1 = ord($str{1});
  476. if ($c0 == 0xfe && $c1 == 0xff)
  477. {
  478. $str = substr($str, 2);
  479. }
  480. elseif ($c0 == 0xff && $c1 == 0xfe)
  481. {
  482. $str = substr($str, 2);
  483. $bom_be = false;
  484. }
  485. $len = strlen($str);
  486. $newstr = '';
  487. for($i = 0; $i < $len; $i += 2)
  488. {
  489. if ($bom_be)
  490. {
  491. $val = ord($str{$i}) << 4;
  492. $val += ord($str{$i + 1});
  493. }
  494. else
  495. {
  496. $val = ord($str{$i + 1}) << 4;
  497. $val += ord($str{$i});
  498. }
  499. $newstr .= ($val == 0x228) ? "\n" : chr($val);
  500. }
  501. return $newstr;
  502. }
  503. /**
  504. * Get character count. First try mbstring, then iconv, finally strlen
  505. *
  506. * @param string $value
  507. * @param string $enc Encoding
  508. * @return int Character count
  509. */
  510. public static function CountCharacters($value, $enc = 'UTF-8')
  511. {
  512. if (self :: getIsIconvEnabled())
  513. {
  514. return iconv_strlen($value, $enc);
  515. }
  516. if (self :: getIsMbstringEnabled())
  517. {
  518. return mb_strlen($value, $enc);
  519. }
  520. // else strlen
  521. return strlen($value);
  522. }
  523. /**
  524. * Get a substring of a UTF-8 encoded string
  525. *
  526. * @param string $pValue UTF-8 encoded string
  527. * @param int $start Start offset
  528. * @param int $length Maximum number of characters in substring
  529. * @return string
  530. */
  531. public static function Substring($pValue = '', $pStart = 0, $pLength = 0)
  532. {
  533. if (self :: getIsIconvEnabled())
  534. {
  535. return iconv_substr($pValue, $pStart, $pLength, 'UTF-8');
  536. }
  537. if (self :: getIsMbstringEnabled())
  538. {
  539. return mb_substr($pValue, $pStart, $pLength, 'UTF-8');
  540. }
  541. // else substr
  542. return substr($pValue, $pStart, $pLength);
  543. }
  544. /**
  545. * Identify whether a string contains a fractional numeric value,
  546. * and convert it to a numeric if it is
  547. *
  548. * @param string &$operand string value to test
  549. * @return boolean
  550. */
  551. public static function convertToNumberIfFraction(&$operand)
  552. {
  553. if (preg_match('/^' . self :: STRING_REGEXP_FRACTION . '$/i', $operand, $match))
  554. {
  555. $sign = ($match[1] == '-') ? '-' : '+';
  556. $fractionFormula = '=' . $sign . $match[2] . $sign . $match[3];
  557. $operand = PHPExcel_Calculation :: getInstance()->_calculateFormulaValue($fractionFormula);
  558. return true;
  559. }
  560. return false;
  561. } // function convertToNumberIfFraction()
  562. /**
  563. * Get the decimal separator. If it has not yet been set explicitly, try to obtain number
  564. * formatting information from locale.
  565. *
  566. * @return string
  567. */
  568. public static function getDecimalSeparator()
  569. {
  570. if (! isset(self :: $_decimalSeparator))
  571. {
  572. $localeconv = localeconv();
  573. self :: $_decimalSeparator = $localeconv['decimal_point'] != '' ? $localeconv['decimal_point'] : $localeconv['mon_decimal_point'];
  574. if (self :: $_decimalSeparator == '')
  575. {
  576. // Default to .
  577. self :: $_decimalSeparator = '.';
  578. }
  579. }
  580. return self :: $_decimalSeparator;
  581. }
  582. /**
  583. * Set the decimal separator. Only used by PHPExcel_Style_NumberFormat::toFormattedString()
  584. * to format output by PHPExcel_Writer_HTML and PHPExcel_Writer_PDF
  585. *
  586. * @param string $pValue Character for decimal separator
  587. */
  588. public static function setDecimalSeparator($pValue = '.')
  589. {
  590. self :: $_decimalSeparator = $pValue;
  591. }
  592. /**
  593. * Get the thousands separator. If it has not yet been set explicitly, try to obtain number
  594. * formatting information from locale.
  595. *
  596. * @return string
  597. */
  598. public static function getThousandsSeparator()
  599. {
  600. if (! isset(self :: $_thousandsSeparator))
  601. {
  602. $localeconv = localeconv();
  603. self :: $_thousandsSeparator = $localeconv['thousands_sep'] != '' ? $localeconv['thousands_sep'] : $localeconv['mon_thousands_sep'];
  604. }
  605. return self :: $_thousandsSeparator;
  606. }
  607. /**
  608. * Set the thousands separator. Only used by PHPExcel_Style_NumberFormat::toFormattedString()
  609. * to format output by PHPExcel_Writer_HTML and PHPExcel_Writer_PDF
  610. *
  611. * @param string $pValue Character for thousands separator
  612. */
  613. public static function setThousandsSeparator($pValue = ',')
  614. {
  615. self :: $_thousandsSeparator = $pValue;
  616. }
  617. /**
  618. * Get the currency code. If it has not yet been set explicitly, try to obtain the
  619. * symbol information from locale.
  620. *
  621. * @return string
  622. */
  623. public static function getCurrencyCode()
  624. {
  625. if (! isset(self :: $_currencyCode))
  626. {
  627. $localeconv = localeconv();
  628. self :: $_currencyCode = $localeconv['currency_symbol'] != '' ? $localeconv['currency_symbol'] : $localeconv['int_curr_symbol'];
  629. if (self :: $_currencyCode == '')
  630. {
  631. // Default to $
  632. self :: $_currencyCode = '$';
  633. }
  634. }
  635. return self :: $_currencyCode;
  636. }
  637. /**
  638. * Set the currency code. Only used by PHPExcel_Style_NumberFormat::toFormattedString()
  639. * to format output by PHPExcel_Writer_HTML and PHPExcel_Writer_PDF
  640. *
  641. * @param string $pValue Character for currency code
  642. */
  643. public static function setCurrencyCode($pValue = '$')
  644. {
  645. self :: $_currencyCode = $pValue;
  646. }
  647. /**
  648. * Convert SYLK encoded string to UTF-8
  649. *
  650. * @param string $pValue
  651. * @return string UTF-8 encoded string
  652. */
  653. public static function SYLKtoUTF8($pValue = '')
  654. {
  655. // If there is no escape character in the string there is nothing to do
  656. if (strpos($pValue, '') === false)
  657. {
  658. return $pValue;
  659. }
  660. foreach (self :: $_SYLKCharacters as $k => $v)
  661. {
  662. $pValue = str_replace($k, $v, $pValue);
  663. }
  664. return $pValue;
  665. }
  666. }