String.php 22 KB

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