String.php 27 KB

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