PHPExcelString.Class.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: phperstar
  5. * Date: 2020/8/11
  6. * Time: 3:58 PM
  7. */
  8. namespace Util\PHPExcel\Shared;
  9. class PHPExcelString
  10. {
  11. /** Constants */
  12. /** Regular Expressions */
  13. // Fraction
  14. const STRING_REGEXP_FRACTION = '(-?)(\d+)\s+(\d+\/\d+)';
  15. /**
  16. * Control characters array
  17. *
  18. * @var string[]
  19. */
  20. private static $controlCharacters = array();
  21. /**
  22. * SYLK Characters array
  23. *
  24. * $var array
  25. */
  26. private static $SYLKCharacters = array();
  27. /**
  28. * Decimal separator
  29. *
  30. * @var string
  31. */
  32. private static $decimalSeparator;
  33. /**
  34. * Thousands separator
  35. *
  36. * @var string
  37. */
  38. private static $thousandsSeparator;
  39. /**
  40. * Currency code
  41. *
  42. * @var string
  43. */
  44. private static $currencyCode;
  45. /**
  46. * Is mbstring extension avalable?
  47. *
  48. * @var boolean
  49. */
  50. private static $isMbstringEnabled;
  51. /**
  52. * Is iconv extension avalable?
  53. *
  54. * @var boolean
  55. */
  56. private static $isIconvEnabled;
  57. /**
  58. * Build control characters array
  59. */
  60. private static function buildControlCharacters()
  61. {
  62. for ($i = 0; $i <= 31; ++$i) {
  63. if ($i != 9 && $i != 10 && $i != 13) {
  64. $find = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_';
  65. $replace = chr($i);
  66. self::$controlCharacters[$find] = $replace;
  67. }
  68. }
  69. }
  70. /**
  71. * Build SYLK characters array
  72. */
  73. private static function buildSYLKCharacters()
  74. {
  75. self::$SYLKCharacters = array(
  76. "\x1B 0" => chr(0),
  77. "\x1B 1" => chr(1),
  78. "\x1B 2" => chr(2),
  79. "\x1B 3" => chr(3),
  80. "\x1B 4" => chr(4),
  81. "\x1B 5" => chr(5),
  82. "\x1B 6" => chr(6),
  83. "\x1B 7" => chr(7),
  84. "\x1B 8" => chr(8),
  85. "\x1B 9" => chr(9),
  86. "\x1B :" => chr(10),
  87. "\x1B ;" => chr(11),
  88. "\x1B <" => chr(12),
  89. "\x1B :" => chr(13),
  90. "\x1B >" => chr(14),
  91. "\x1B ?" => chr(15),
  92. "\x1B!0" => chr(16),
  93. "\x1B!1" => chr(17),
  94. "\x1B!2" => chr(18),
  95. "\x1B!3" => chr(19),
  96. "\x1B!4" => chr(20),
  97. "\x1B!5" => chr(21),
  98. "\x1B!6" => chr(22),
  99. "\x1B!7" => chr(23),
  100. "\x1B!8" => chr(24),
  101. "\x1B!9" => chr(25),
  102. "\x1B!:" => chr(26),
  103. "\x1B!;" => chr(27),
  104. "\x1B!<" => chr(28),
  105. "\x1B!=" => chr(29),
  106. "\x1B!>" => chr(30),
  107. "\x1B!?" => chr(31),
  108. "\x1B'?" => chr(127),
  109. "\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. return self::$isMbstringEnabled;
  243. }
  244. self::$isMbstringEnabled = function_exists('mb_convert_encoding') ?
  245. 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. return self::$isIconvEnabled;
  257. }
  258. // Fail if iconv doesn't exist
  259. if (!function_exists('iconv')) {
  260. self::$isIconvEnabled = false;
  261. return false;
  262. }
  263. // Sometimes iconv is not working, and e.g. iconv('UTF-8', 'UTF-16LE', 'x') just returns false,
  264. if (!@iconv('UTF-8', 'UTF-16LE', 'x')) {
  265. self::$isIconvEnabled = false;
  266. return false;
  267. }
  268. // Sometimes iconv_substr('A', 0, 1, 'UTF-8') just returns false in PHP 5.2.0
  269. // we cannot use iconv in that case either (http://bugs.php.net/bug.php?id=37773)
  270. if (!@iconv_substr('A', 0, 1, 'UTF-8')) {
  271. self::$isIconvEnabled = false;
  272. return false;
  273. }
  274. // CUSTOM: IBM AIX iconv() does not work
  275. if (defined('PHP_OS') && @stristr(PHP_OS, 'AIX') && defined('ICONV_IMPL') && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && defined('ICONV_VERSION') && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
  276. self::$isIconvEnabled = false;
  277. return false;
  278. }
  279. // If we reach here no problems were detected with iconv
  280. self::$isIconvEnabled = true;
  281. return true;
  282. }
  283. public static function buildCharacterSets()
  284. {
  285. if (empty(self::$controlCharacters)) {
  286. self::buildControlCharacters();
  287. }
  288. if (empty(self::$SYLKCharacters)) {
  289. self::buildSYLKCharacters();
  290. }
  291. }
  292. /**
  293. * Convert from OpenXML escaped control character to PHP control character
  294. *
  295. * Excel 2007 team:
  296. * ----------------
  297. * That's correct, control characters are stored directly in the shared-strings table.
  298. * We do encode characters that cannot be represented in XML using the following escape sequence:
  299. * _xHHHH_ where H represents a hexadecimal character in the character's value...
  300. * So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
  301. * element or in the shared string <t> element.
  302. *
  303. * @param string $value Value to unescape
  304. * @return string
  305. */
  306. public static function ControlCharacterOOXML2PHP($value = '')
  307. {
  308. return str_replace(array_keys(self::$controlCharacters), array_values(self::$controlCharacters), $value);
  309. }
  310. /**
  311. * Convert from PHP control character to OpenXML escaped control character
  312. *
  313. * Excel 2007 team:
  314. * ----------------
  315. * That's correct, control characters are stored directly in the shared-strings table.
  316. * We do encode characters that cannot be represented in XML using the following escape sequence:
  317. * _xHHHH_ where H represents a hexadecimal character in the character's value...
  318. * So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
  319. * element or in the shared string <t> element.
  320. *
  321. * @param string $value Value to escape
  322. * @return string
  323. */
  324. public static function ControlCharacterPHP2OOXML($value = '')
  325. {
  326. return str_replace(array_values(self::$controlCharacters), array_keys(self::$controlCharacters), $value);
  327. }
  328. /**
  329. * Try to sanitize UTF8, stripping invalid byte sequences. Not perfect. Does not surrogate characters.
  330. *
  331. * @param string $value
  332. * @return string
  333. */
  334. public static function SanitizeUTF8($value)
  335. {
  336. if (self::getIsIconvEnabled()) {
  337. $value = @iconv('UTF-8', 'UTF-8', $value);
  338. return $value;
  339. }
  340. if (self::getIsMbstringEnabled()) {
  341. $value = mb_convert_encoding($value, 'UTF-8', 'UTF-8');
  342. return $value;
  343. }
  344. // else, no conversion
  345. return $value;
  346. }
  347. /**
  348. * Check if a string contains UTF8 data
  349. *
  350. * @param string $value
  351. * @return boolean
  352. */
  353. public static function IsUTF8($value = '')
  354. {
  355. return $value === '' || preg_match('/^./su', $value) === 1;
  356. }
  357. /**
  358. * Formats a numeric value as a string for output in various output writers forcing
  359. * point as decimal separator in case locale is other than English.
  360. *
  361. * @param mixed $value
  362. * @return string
  363. */
  364. public static function FormatNumber($value)
  365. {
  366. if (is_float($value)) {
  367. return str_replace(',', '.', $value);
  368. }
  369. return (string) $value;
  370. }
  371. /**
  372. * Converts a UTF-8 string into BIFF8 Unicode string data (8-bit string length)
  373. * Writes the string using uncompressed notation, no rich text, no Asian phonetics
  374. * If mbstring extension is not available, ASCII is assumed, and compressed notation is used
  375. * although this will give wrong results for non-ASCII strings
  376. * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3
  377. *
  378. * @param string $value UTF-8 encoded string
  379. * @param mixed[] $arrcRuns Details of rich text runs in $value
  380. * @return string
  381. */
  382. public static function UTF8toBIFF8UnicodeShort($value, $arrcRuns = array())
  383. {
  384. // character count
  385. $ln = self::CountCharacters($value, 'UTF-8');
  386. // option flags
  387. if (empty($arrcRuns)) {
  388. $opt = (self::getIsIconvEnabled() || self::getIsMbstringEnabled()) ?
  389. 0x0001 : 0x0000;
  390. $data = pack('CC', $ln, $opt);
  391. // characters
  392. $data .= self::ConvertEncoding($value, 'UTF-16LE', 'UTF-8');
  393. } else {
  394. $data = pack('vC', $ln, 0x09);
  395. $data .= pack('v', count($arrcRuns));
  396. // characters
  397. $data .= self::ConvertEncoding($value, 'UTF-16LE', 'UTF-8');
  398. foreach ($arrcRuns as $cRun) {
  399. $data .= pack('v', $cRun['strlen']);
  400. $data .= pack('v', $cRun['fontidx']);
  401. }
  402. }
  403. return $data;
  404. }
  405. /**
  406. * Converts a UTF-8 string into BIFF8 Unicode string data (16-bit string length)
  407. * Writes the string using uncompressed notation, no rich text, no Asian phonetics
  408. * If mbstring extension is not available, ASCII is assumed, and compressed notation is used
  409. * although this will give wrong results for non-ASCII strings
  410. * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3
  411. *
  412. * @param string $value UTF-8 encoded string
  413. * @return string
  414. */
  415. public static function UTF8toBIFF8UnicodeLong($value)
  416. {
  417. // character count
  418. $ln = self::CountCharacters($value, 'UTF-8');
  419. // option flags
  420. $opt = (self::getIsIconvEnabled() || self::getIsMbstringEnabled()) ?
  421. 0x0001 : 0x0000;
  422. // characters
  423. $chars = self::ConvertEncoding($value, 'UTF-16LE', 'UTF-8');
  424. $data = pack('vC', $ln, $opt) . $chars;
  425. return $data;
  426. }
  427. /**
  428. * Convert string from one encoding to another. First try mbstring, then iconv, finally strlen
  429. *
  430. * @param string $value
  431. * @param string $to Encoding to convert to, e.g. 'UTF-8'
  432. * @param string $from Encoding to convert from, e.g. 'UTF-16LE'
  433. * @return string
  434. */
  435. public static function ConvertEncoding($value, $to, $from)
  436. {
  437. if (self::getIsIconvEnabled()) {
  438. return iconv($from, $to, $value);
  439. }
  440. if (self::getIsMbstringEnabled()) {
  441. return mb_convert_encoding($value, $to, $from);
  442. }
  443. if ($from == 'UTF-16LE') {
  444. return self::utf16_decode($value, false);
  445. } elseif ($from == 'UTF-16BE') {
  446. return self::utf16_decode($value);
  447. }
  448. // else, no conversion
  449. return $value;
  450. }
  451. /**
  452. * Decode UTF-16 encoded strings.
  453. *
  454. * Can handle both BOM'ed data and un-BOM'ed data.
  455. * Assumes Big-Endian byte order if no BOM is available.
  456. * This function was taken from http://php.net/manual/en/function.utf8-decode.php
  457. * and $bom_be parameter added.
  458. *
  459. * @param string $str UTF-16 encoded data to decode.
  460. * @return string UTF-8 / ISO encoded data.
  461. * @access public
  462. * @version 0.2 / 2010-05-13
  463. * @author Rasmus Andersson {@link http://rasmusandersson.se/}
  464. * @author vadik56
  465. */
  466. public static function utf16_decode($str, $bom_be = true)
  467. {
  468. if (strlen($str) < 2) {
  469. return $str;
  470. }
  471. $c0 = ord($str{0});
  472. $c1 = ord($str{1});
  473. if ($c0 == 0xfe && $c1 == 0xff) {
  474. $str = substr($str, 2);
  475. } elseif ($c0 == 0xff && $c1 == 0xfe) {
  476. $str = substr($str, 2);
  477. $bom_be = false;
  478. }
  479. $len = strlen($str);
  480. $newstr = '';
  481. for ($i=0; $i<$len; $i+=2) {
  482. if ($bom_be) {
  483. $val = ord($str{$i}) << 4;
  484. $val += ord($str{$i+1});
  485. } else {
  486. $val = ord($str{$i+1}) << 4;
  487. $val += ord($str{$i});
  488. }
  489. $newstr .= ($val == 0x228) ? "\n" : chr($val);
  490. }
  491. return $newstr;
  492. }
  493. /**
  494. * Get character count. First try mbstring, then iconv, finally strlen
  495. *
  496. * @param string $value
  497. * @param string $enc Encoding
  498. * @return int Character count
  499. */
  500. public static function CountCharacters($value, $enc = 'UTF-8')
  501. {
  502. if (self::getIsMbstringEnabled()) {
  503. return mb_strlen($value, $enc);
  504. }
  505. if (self::getIsIconvEnabled()) {
  506. return iconv_strlen($value, $enc);
  507. }
  508. // else strlen
  509. return strlen($value);
  510. }
  511. /**
  512. * Get a substring of a UTF-8 encoded string. First try mbstring, then iconv, finally strlen
  513. *
  514. * @param string $pValue UTF-8 encoded string
  515. * @param int $pStart Start offset
  516. * @param int $pLength Maximum number of characters in substring
  517. * @return string
  518. */
  519. public static function Substring($pValue = '', $pStart = 0, $pLength = 0)
  520. {
  521. if (self::getIsMbstringEnabled()) {
  522. return mb_substr($pValue, $pStart, $pLength, 'UTF-8');
  523. }
  524. if (self::getIsIconvEnabled()) {
  525. return iconv_substr($pValue, $pStart, $pLength, 'UTF-8');
  526. }
  527. // else substr
  528. return substr($pValue, $pStart, $pLength);
  529. }
  530. /**
  531. * Convert a UTF-8 encoded string to upper case
  532. *
  533. * @param string $pValue UTF-8 encoded string
  534. * @return string
  535. */
  536. public static function StrToUpper($pValue = '')
  537. {
  538. if (function_exists('mb_convert_case')) {
  539. return mb_convert_case($pValue, MB_CASE_UPPER, "UTF-8");
  540. }
  541. return strtoupper($pValue);
  542. }
  543. /**
  544. * Convert a UTF-8 encoded string to lower case
  545. *
  546. * @param string $pValue UTF-8 encoded string
  547. * @return string
  548. */
  549. public static function StrToLower($pValue = '')
  550. {
  551. if (function_exists('mb_convert_case')) {
  552. return mb_convert_case($pValue, MB_CASE_LOWER, "UTF-8");
  553. }
  554. return strtolower($pValue);
  555. }
  556. /**
  557. * Convert a UTF-8 encoded string to title/proper case
  558. * (uppercase every first character in each word, lower case all other characters)
  559. *
  560. * @param string $pValue UTF-8 encoded string
  561. * @return string
  562. */
  563. public static function StrToTitle($pValue = '')
  564. {
  565. if (function_exists('mb_convert_case')) {
  566. return mb_convert_case($pValue, MB_CASE_TITLE, "UTF-8");
  567. }
  568. return ucwords($pValue);
  569. }
  570. public static function mb_is_upper($char)
  571. {
  572. return mb_strtolower($char, "UTF-8") != $char;
  573. }
  574. public static function mb_str_split($string)
  575. {
  576. # Split at all position not after the start: ^
  577. # and not before the end: $
  578. return preg_split('/(?<!^)(?!$)/u', $string);
  579. }
  580. /**
  581. * Reverse the case of a string, so that all uppercase characters become lowercase
  582. * and all lowercase characters become uppercase
  583. *
  584. * @param string $pValue UTF-8 encoded string
  585. * @return string
  586. */
  587. public static function StrCaseReverse($pValue = '')
  588. {
  589. if (self::getIsMbstringEnabled()) {
  590. $characters = self::mb_str_split($pValue);
  591. foreach ($characters as &$character) {
  592. if (self::mb_is_upper($character)) {
  593. $character = mb_strtolower($character, 'UTF-8');
  594. } else {
  595. $character = mb_strtoupper($character, 'UTF-8');
  596. }
  597. }
  598. return implode('', $characters);
  599. }
  600. return strtolower($pValue) ^ strtoupper($pValue) ^ $pValue;
  601. }
  602. /**
  603. * Identify whether a string contains a fractional numeric value,
  604. * and convert it to a numeric if it is
  605. *
  606. * @param string &$operand string value to test
  607. * @return boolean
  608. */
  609. public static function convertToNumberIfFraction(&$operand)
  610. {
  611. if (preg_match('/^'.self::STRING_REGEXP_FRACTION.'$/i', $operand, $match)) {
  612. $sign = ($match[1] == '-') ? '-' : '+';
  613. $fractionFormula = '='.$sign.$match[2].$sign.$match[3];
  614. $operand = PHPExcel_Calculation::getInstance()->_calculateFormulaValue($fractionFormula);
  615. return true;
  616. }
  617. return false;
  618. } // function convertToNumberIfFraction()
  619. /**
  620. * Get the decimal separator. If it has not yet been set explicitly, try to obtain number
  621. * formatting information from locale.
  622. *
  623. * @return string
  624. */
  625. public static function getDecimalSeparator()
  626. {
  627. if (!isset(self::$decimalSeparator)) {
  628. $localeconv = localeconv();
  629. self::$decimalSeparator = ($localeconv['decimal_point'] != '')
  630. ? $localeconv['decimal_point'] : $localeconv['mon_decimal_point'];
  631. if (self::$decimalSeparator == '') {
  632. // Default to .
  633. self::$decimalSeparator = '.';
  634. }
  635. }
  636. return self::$decimalSeparator;
  637. }
  638. /**
  639. * Set the decimal separator. Only used by PHPExcel_Style_NumberFormat::toFormattedString()
  640. * to format output by PHPExcel_Writer_HTML and PHPExcel_Writer_PDF
  641. *
  642. * @param string $pValue Character for decimal separator
  643. */
  644. public static function setDecimalSeparator($pValue = '.')
  645. {
  646. self::$decimalSeparator = $pValue;
  647. }
  648. /**
  649. * Get the thousands separator. If it has not yet been set explicitly, try to obtain number
  650. * formatting information from locale.
  651. *
  652. * @return string
  653. */
  654. public static function getThousandsSeparator()
  655. {
  656. if (!isset(self::$thousandsSeparator)) {
  657. $localeconv = localeconv();
  658. self::$thousandsSeparator = ($localeconv['thousands_sep'] != '')
  659. ? $localeconv['thousands_sep'] : $localeconv['mon_thousands_sep'];
  660. if (self::$thousandsSeparator == '') {
  661. // Default to .
  662. self::$thousandsSeparator = ',';
  663. }
  664. }
  665. return self::$thousandsSeparator;
  666. }
  667. /**
  668. * Set the thousands separator. Only used by PHPExcel_Style_NumberFormat::toFormattedString()
  669. * to format output by PHPExcel_Writer_HTML and PHPExcel_Writer_PDF
  670. *
  671. * @param string $pValue Character for thousands separator
  672. */
  673. public static function setThousandsSeparator($pValue = ',')
  674. {
  675. self::$thousandsSeparator = $pValue;
  676. }
  677. /**
  678. * Get the currency code. If it has not yet been set explicitly, try to obtain the
  679. * symbol information from locale.
  680. *
  681. * @return string
  682. */
  683. public static function getCurrencyCode()
  684. {
  685. if (!isset(self::$currencyCode)) {
  686. $localeconv = localeconv();
  687. self::$currencyCode = ($localeconv['currency_symbol'] != '')
  688. ? $localeconv['currency_symbol'] : $localeconv['int_curr_symbol'];
  689. if (self::$currencyCode == '') {
  690. // Default to $
  691. self::$currencyCode = '$';
  692. }
  693. }
  694. return self::$currencyCode;
  695. }
  696. /**
  697. * Set the currency code. Only used by PHPExcel_Style_NumberFormat::toFormattedString()
  698. * to format output by PHPExcel_Writer_HTML and PHPExcel_Writer_PDF
  699. *
  700. * @param string $pValue Character for currency code
  701. */
  702. public static function setCurrencyCode($pValue = '$')
  703. {
  704. self::$currencyCode = $pValue;
  705. }
  706. /**
  707. * Convert SYLK encoded string to UTF-8
  708. *
  709. * @param string $pValue
  710. * @return string UTF-8 encoded string
  711. */
  712. public static function SYLKtoUTF8($pValue = '')
  713. {
  714. // If there is no escape character in the string there is nothing to do
  715. if (strpos($pValue, '') === false) {
  716. return $pValue;
  717. }
  718. foreach (self::$SYLKCharacters as $k => $v) {
  719. $pValue = str_replace($k, $v, $pValue);
  720. }
  721. return $pValue;
  722. }
  723. /**
  724. * Retrieve any leading numeric part of a string, or return the full string if no leading numeric
  725. * (handles basic integer or float, but not exponent or non decimal)
  726. *
  727. * @param string $value
  728. * @return mixed string or only the leading numeric part of the string
  729. */
  730. public static function testStringAsNumeric($value)
  731. {
  732. if (is_numeric($value)) {
  733. return $value;
  734. }
  735. $v = floatval($value);
  736. return (is_numeric(substr($value, 0, strlen($v)))) ? $v : $value;
  737. }
  738. }