1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Shared;
|
|
|
4 |
|
|
|
5 |
class StringHelper
|
|
|
6 |
{
|
|
|
7 |
/**
|
|
|
8 |
* Control characters array.
|
|
|
9 |
*
|
|
|
10 |
* @var string[]
|
|
|
11 |
*/
|
|
|
12 |
private static $controlCharacters = [];
|
|
|
13 |
|
|
|
14 |
/**
|
|
|
15 |
* SYLK Characters array.
|
|
|
16 |
*
|
|
|
17 |
* @var array
|
|
|
18 |
*/
|
|
|
19 |
private static $SYLKCharacters = [];
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Decimal separator.
|
|
|
23 |
*
|
|
|
24 |
* @var ?string
|
|
|
25 |
*/
|
|
|
26 |
private static $decimalSeparator;
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Thousands separator.
|
|
|
30 |
*
|
|
|
31 |
* @var ?string
|
|
|
32 |
*/
|
|
|
33 |
private static $thousandsSeparator;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Currency code.
|
|
|
37 |
*
|
|
|
38 |
* @var string
|
|
|
39 |
*/
|
|
|
40 |
private static $currencyCode;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Is iconv extension avalable?
|
|
|
44 |
*
|
|
|
45 |
* @var ?bool
|
|
|
46 |
*/
|
|
|
47 |
private static $isIconvEnabled;
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* iconv options.
|
|
|
51 |
*
|
|
|
52 |
* @var string
|
|
|
53 |
*/
|
|
|
54 |
private static $iconvOptions = '//IGNORE//TRANSLIT';
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Build control characters array.
|
|
|
58 |
*/
|
|
|
59 |
private static function buildControlCharacters(): void
|
|
|
60 |
{
|
|
|
61 |
for ($i = 0; $i <= 31; ++$i) {
|
|
|
62 |
if ($i != 9 && $i != 10 && $i != 13) {
|
|
|
63 |
$find = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_';
|
|
|
64 |
$replace = chr($i);
|
|
|
65 |
self::$controlCharacters[$find] = $replace;
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Build SYLK characters array.
|
|
|
72 |
*/
|
|
|
73 |
private static function buildSYLKCharacters(): void
|
|
|
74 |
{
|
|
|
75 |
self::$SYLKCharacters = [
|
|
|
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 |
/**
|
|
|
236 |
* Get whether iconv extension is available.
|
|
|
237 |
*
|
|
|
238 |
* @return bool
|
|
|
239 |
*/
|
|
|
240 |
public static function getIsIconvEnabled()
|
|
|
241 |
{
|
|
|
242 |
if (isset(self::$isIconvEnabled)) {
|
|
|
243 |
return self::$isIconvEnabled;
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
// Assume no problems with iconv
|
|
|
247 |
self::$isIconvEnabled = true;
|
|
|
248 |
|
|
|
249 |
// Fail if iconv doesn't exist
|
|
|
250 |
if (!function_exists('iconv')) {
|
|
|
251 |
self::$isIconvEnabled = false;
|
|
|
252 |
} elseif (!@iconv('UTF-8', 'UTF-16LE', 'x')) {
|
|
|
253 |
// Sometimes iconv is not working, and e.g. iconv('UTF-8', 'UTF-16LE', 'x') just returns false,
|
|
|
254 |
self::$isIconvEnabled = false;
|
|
|
255 |
} elseif (defined('PHP_OS') && @stristr(PHP_OS, 'AIX') && defined('ICONV_IMPL') && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && defined('ICONV_VERSION') && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
|
|
|
256 |
// CUSTOM: IBM AIX iconv() does not work
|
|
|
257 |
self::$isIconvEnabled = false;
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
// Deactivate iconv default options if they fail (as seen on IMB i)
|
|
|
261 |
if (self::$isIconvEnabled && !@iconv('UTF-8', 'UTF-16LE' . self::$iconvOptions, 'x')) {
|
|
|
262 |
self::$iconvOptions = '';
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
return self::$isIconvEnabled;
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
private static function buildCharacterSets(): void
|
|
|
269 |
{
|
|
|
270 |
if (empty(self::$controlCharacters)) {
|
|
|
271 |
self::buildControlCharacters();
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
if (empty(self::$SYLKCharacters)) {
|
|
|
275 |
self::buildSYLKCharacters();
|
|
|
276 |
}
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
/**
|
|
|
280 |
* Convert from OpenXML escaped control character to PHP control character.
|
|
|
281 |
*
|
|
|
282 |
* Excel 2007 team:
|
|
|
283 |
* ----------------
|
|
|
284 |
* That's correct, control characters are stored directly in the shared-strings table.
|
|
|
285 |
* We do encode characters that cannot be represented in XML using the following escape sequence:
|
|
|
286 |
* _xHHHH_ where H represents a hexadecimal character in the character's value...
|
|
|
287 |
* So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
|
|
|
288 |
* element or in the shared string <t> element.
|
|
|
289 |
*
|
|
|
290 |
* @param string $textValue Value to unescape
|
|
|
291 |
*
|
|
|
292 |
* @return string
|
|
|
293 |
*/
|
|
|
294 |
public static function controlCharacterOOXML2PHP($textValue)
|
|
|
295 |
{
|
|
|
296 |
self::buildCharacterSets();
|
|
|
297 |
|
|
|
298 |
return str_replace(array_keys(self::$controlCharacters), array_values(self::$controlCharacters), $textValue);
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
/**
|
|
|
302 |
* Convert from PHP control character to OpenXML escaped control character.
|
|
|
303 |
*
|
|
|
304 |
* Excel 2007 team:
|
|
|
305 |
* ----------------
|
|
|
306 |
* That's correct, control characters are stored directly in the shared-strings table.
|
|
|
307 |
* We do encode characters that cannot be represented in XML using the following escape sequence:
|
|
|
308 |
* _xHHHH_ where H represents a hexadecimal character in the character's value...
|
|
|
309 |
* So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
|
|
|
310 |
* element or in the shared string <t> element.
|
|
|
311 |
*
|
|
|
312 |
* @param string $textValue Value to escape
|
|
|
313 |
*
|
|
|
314 |
* @return string
|
|
|
315 |
*/
|
|
|
316 |
public static function controlCharacterPHP2OOXML($textValue)
|
|
|
317 |
{
|
|
|
318 |
self::buildCharacterSets();
|
|
|
319 |
|
|
|
320 |
return str_replace(array_values(self::$controlCharacters), array_keys(self::$controlCharacters), $textValue);
|
|
|
321 |
}
|
|
|
322 |
|
|
|
323 |
/**
|
|
|
324 |
* Try to sanitize UTF8, replacing invalid sequences with Unicode substitution characters.
|
|
|
325 |
*/
|
|
|
326 |
public static function sanitizeUTF8(string $textValue): string
|
|
|
327 |
{
|
|
|
328 |
$textValue = str_replace(["\xef\xbf\xbe", "\xef\xbf\xbf"], "\xef\xbf\xbd", $textValue);
|
|
|
329 |
$subst = mb_substitute_character(); // default is question mark
|
|
|
330 |
mb_substitute_character(65533); // Unicode substitution character
|
|
|
331 |
// Phpstan does not think this can return false.
|
|
|
332 |
$returnValue = mb_convert_encoding($textValue, 'UTF-8', 'UTF-8');
|
|
|
333 |
mb_substitute_character(/** @scrutinizer ignore-type */ $subst);
|
|
|
334 |
|
|
|
335 |
return self::returnString($returnValue);
|
|
|
336 |
}
|
|
|
337 |
|
|
|
338 |
/**
|
|
|
339 |
* Strictly to satisfy Scrutinizer.
|
|
|
340 |
*
|
|
|
341 |
* @param mixed $value
|
|
|
342 |
*/
|
|
|
343 |
private static function returnString($value): string
|
|
|
344 |
{
|
|
|
345 |
return is_string($value) ? $value : '';
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
/**
|
|
|
349 |
* Check if a string contains UTF8 data.
|
|
|
350 |
*/
|
|
|
351 |
public static function isUTF8(string $textValue): bool
|
|
|
352 |
{
|
|
|
353 |
return $textValue === self::sanitizeUTF8($textValue);
|
|
|
354 |
}
|
|
|
355 |
|
|
|
356 |
/**
|
|
|
357 |
* Formats a numeric value as a string for output in various output writers forcing
|
|
|
358 |
* point as decimal separator in case locale is other than English.
|
|
|
359 |
*
|
|
|
360 |
* @param float|int|string $numericValue
|
|
|
361 |
*/
|
|
|
362 |
public static function formatNumber($numericValue): string
|
|
|
363 |
{
|
|
|
364 |
if (is_float($numericValue)) {
|
|
|
365 |
return str_replace(',', '.', (string) $numericValue);
|
|
|
366 |
}
|
|
|
367 |
|
|
|
368 |
return (string) $numericValue;
|
|
|
369 |
}
|
|
|
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 $textValue UTF-8 encoded string
|
|
|
379 |
* @param mixed[] $arrcRuns Details of rich text runs in $value
|
|
|
380 |
*/
|
|
|
381 |
public static function UTF8toBIFF8UnicodeShort(string $textValue, array $arrcRuns = []): string
|
|
|
382 |
{
|
|
|
383 |
// character count
|
|
|
384 |
$ln = self::countCharacters($textValue, 'UTF-8');
|
|
|
385 |
// option flags
|
|
|
386 |
if (empty($arrcRuns)) {
|
|
|
387 |
$data = pack('CC', $ln, 0x0001);
|
|
|
388 |
// characters
|
|
|
389 |
$data .= self::convertEncoding($textValue, 'UTF-16LE', 'UTF-8');
|
|
|
390 |
} else {
|
|
|
391 |
$data = pack('vC', $ln, 0x09);
|
|
|
392 |
$data .= pack('v', count($arrcRuns));
|
|
|
393 |
// characters
|
|
|
394 |
$data .= self::convertEncoding($textValue, 'UTF-16LE', 'UTF-8');
|
|
|
395 |
foreach ($arrcRuns as $cRun) {
|
|
|
396 |
$data .= pack('v', $cRun['strlen']);
|
|
|
397 |
$data .= pack('v', $cRun['fontidx']);
|
|
|
398 |
}
|
|
|
399 |
}
|
|
|
400 |
|
|
|
401 |
return $data;
|
|
|
402 |
}
|
|
|
403 |
|
|
|
404 |
/**
|
|
|
405 |
* Converts a UTF-8 string into BIFF8 Unicode string data (16-bit string length)
|
|
|
406 |
* Writes the string using uncompressed notation, no rich text, no Asian phonetics
|
|
|
407 |
* If mbstring extension is not available, ASCII is assumed, and compressed notation is used
|
|
|
408 |
* although this will give wrong results for non-ASCII strings
|
|
|
409 |
* see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3.
|
|
|
410 |
*
|
|
|
411 |
* @param string $textValue UTF-8 encoded string
|
|
|
412 |
*/
|
|
|
413 |
public static function UTF8toBIFF8UnicodeLong(string $textValue): string
|
|
|
414 |
{
|
|
|
415 |
// character count
|
|
|
416 |
$ln = self::countCharacters($textValue, 'UTF-8');
|
|
|
417 |
|
|
|
418 |
// characters
|
|
|
419 |
$chars = self::convertEncoding($textValue, 'UTF-16LE', 'UTF-8');
|
|
|
420 |
|
|
|
421 |
return pack('vC', $ln, 0x0001) . $chars;
|
|
|
422 |
}
|
|
|
423 |
|
|
|
424 |
/**
|
|
|
425 |
* Convert string from one encoding to another.
|
|
|
426 |
*
|
|
|
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 |
*/
|
|
|
430 |
public static function convertEncoding(string $textValue, string $to, string $from): string
|
|
|
431 |
{
|
|
|
432 |
if (self::getIsIconvEnabled()) {
|
|
|
433 |
$result = iconv($from, $to . self::$iconvOptions, $textValue);
|
|
|
434 |
if (false !== $result) {
|
|
|
435 |
return $result;
|
|
|
436 |
}
|
|
|
437 |
}
|
|
|
438 |
|
|
|
439 |
return self::returnString(mb_convert_encoding($textValue, $to, $from));
|
|
|
440 |
}
|
|
|
441 |
|
|
|
442 |
/**
|
|
|
443 |
* Get character count.
|
|
|
444 |
*
|
|
|
445 |
* @param string $encoding Encoding
|
|
|
446 |
*
|
|
|
447 |
* @return int Character count
|
|
|
448 |
*/
|
|
|
449 |
public static function countCharacters(string $textValue, string $encoding = 'UTF-8'): int
|
|
|
450 |
{
|
|
|
451 |
return mb_strlen($textValue, $encoding);
|
|
|
452 |
}
|
|
|
453 |
|
|
|
454 |
/**
|
|
|
455 |
* Get character count using mb_strwidth rather than mb_strlen.
|
|
|
456 |
*
|
|
|
457 |
* @param string $encoding Encoding
|
|
|
458 |
*
|
|
|
459 |
* @return int Character count
|
|
|
460 |
*/
|
|
|
461 |
public static function countCharactersDbcs(string $textValue, string $encoding = 'UTF-8'): int
|
|
|
462 |
{
|
|
|
463 |
return mb_strwidth($textValue, $encoding);
|
|
|
464 |
}
|
|
|
465 |
|
|
|
466 |
/**
|
|
|
467 |
* Get a substring of a UTF-8 encoded string.
|
|
|
468 |
*
|
|
|
469 |
* @param string $textValue UTF-8 encoded string
|
|
|
470 |
* @param int $offset Start offset
|
|
|
471 |
* @param ?int $length Maximum number of characters in substring
|
|
|
472 |
*/
|
|
|
473 |
public static function substring(string $textValue, int $offset, ?int $length = 0): string
|
|
|
474 |
{
|
|
|
475 |
return mb_substr($textValue, $offset, $length, 'UTF-8');
|
|
|
476 |
}
|
|
|
477 |
|
|
|
478 |
/**
|
|
|
479 |
* Convert a UTF-8 encoded string to upper case.
|
|
|
480 |
*
|
|
|
481 |
* @param string $textValue UTF-8 encoded string
|
|
|
482 |
*/
|
|
|
483 |
public static function strToUpper(string $textValue): string
|
|
|
484 |
{
|
|
|
485 |
return mb_convert_case($textValue, MB_CASE_UPPER, 'UTF-8');
|
|
|
486 |
}
|
|
|
487 |
|
|
|
488 |
/**
|
|
|
489 |
* Convert a UTF-8 encoded string to lower case.
|
|
|
490 |
*
|
|
|
491 |
* @param string $textValue UTF-8 encoded string
|
|
|
492 |
*/
|
|
|
493 |
public static function strToLower(string $textValue): string
|
|
|
494 |
{
|
|
|
495 |
return mb_convert_case($textValue, MB_CASE_LOWER, 'UTF-8');
|
|
|
496 |
}
|
|
|
497 |
|
|
|
498 |
/**
|
|
|
499 |
* Convert a UTF-8 encoded string to title/proper case
|
|
|
500 |
* (uppercase every first character in each word, lower case all other characters).
|
|
|
501 |
*
|
|
|
502 |
* @param string $textValue UTF-8 encoded string
|
|
|
503 |
*/
|
|
|
504 |
public static function strToTitle(string $textValue): string
|
|
|
505 |
{
|
|
|
506 |
return mb_convert_case($textValue, MB_CASE_TITLE, 'UTF-8');
|
|
|
507 |
}
|
|
|
508 |
|
|
|
509 |
public static function mbIsUpper(string $character): bool
|
|
|
510 |
{
|
|
|
511 |
return mb_strtolower($character, 'UTF-8') !== $character;
|
|
|
512 |
}
|
|
|
513 |
|
|
|
514 |
/**
|
|
|
515 |
* Splits a UTF-8 string into an array of individual characters.
|
|
|
516 |
*/
|
|
|
517 |
public static function mbStrSplit(string $string): array
|
|
|
518 |
{
|
|
|
519 |
// Split at all position not after the start: ^
|
|
|
520 |
// and not before the end: $
|
|
|
521 |
$split = preg_split('/(?<!^)(?!$)/u', $string);
|
|
|
522 |
|
|
|
523 |
return ($split === false) ? [] : $split;
|
|
|
524 |
}
|
|
|
525 |
|
|
|
526 |
/**
|
|
|
527 |
* Reverse the case of a string, so that all uppercase characters become lowercase
|
|
|
528 |
* and all lowercase characters become uppercase.
|
|
|
529 |
*
|
|
|
530 |
* @param string $textValue UTF-8 encoded string
|
|
|
531 |
*/
|
|
|
532 |
public static function strCaseReverse(string $textValue): string
|
|
|
533 |
{
|
|
|
534 |
$characters = self::mbStrSplit($textValue);
|
|
|
535 |
foreach ($characters as &$character) {
|
|
|
536 |
if (self::mbIsUpper($character)) {
|
|
|
537 |
$character = mb_strtolower($character, 'UTF-8');
|
|
|
538 |
} else {
|
|
|
539 |
$character = mb_strtoupper($character, 'UTF-8');
|
|
|
540 |
}
|
|
|
541 |
}
|
|
|
542 |
|
|
|
543 |
return implode('', $characters);
|
|
|
544 |
}
|
|
|
545 |
|
|
|
546 |
/**
|
|
|
547 |
* Get the decimal separator. If it has not yet been set explicitly, try to obtain number
|
|
|
548 |
* formatting information from locale.
|
|
|
549 |
*/
|
|
|
550 |
public static function getDecimalSeparator(): string
|
|
|
551 |
{
|
|
|
552 |
if (!isset(self::$decimalSeparator)) {
|
|
|
553 |
$localeconv = localeconv();
|
|
|
554 |
self::$decimalSeparator = ($localeconv['decimal_point'] != '')
|
|
|
555 |
? $localeconv['decimal_point'] : $localeconv['mon_decimal_point'];
|
|
|
556 |
|
|
|
557 |
if (self::$decimalSeparator == '') {
|
|
|
558 |
// Default to .
|
|
|
559 |
self::$decimalSeparator = '.';
|
|
|
560 |
}
|
|
|
561 |
}
|
|
|
562 |
|
|
|
563 |
return self::$decimalSeparator;
|
|
|
564 |
}
|
|
|
565 |
|
|
|
566 |
/**
|
|
|
567 |
* Set the decimal separator. Only used by NumberFormat::toFormattedString()
|
|
|
568 |
* to format output by \PhpOffice\PhpSpreadsheet\Writer\Html and \PhpOffice\PhpSpreadsheet\Writer\Pdf.
|
|
|
569 |
*
|
|
|
570 |
* @param string $separator Character for decimal separator
|
|
|
571 |
*/
|
|
|
572 |
public static function setDecimalSeparator(string $separator): void
|
|
|
573 |
{
|
|
|
574 |
self::$decimalSeparator = $separator;
|
|
|
575 |
}
|
|
|
576 |
|
|
|
577 |
/**
|
|
|
578 |
* Get the thousands separator. If it has not yet been set explicitly, try to obtain number
|
|
|
579 |
* formatting information from locale.
|
|
|
580 |
*/
|
|
|
581 |
public static function getThousandsSeparator(): string
|
|
|
582 |
{
|
|
|
583 |
if (!isset(self::$thousandsSeparator)) {
|
|
|
584 |
$localeconv = localeconv();
|
|
|
585 |
self::$thousandsSeparator = ($localeconv['thousands_sep'] != '')
|
|
|
586 |
? $localeconv['thousands_sep'] : $localeconv['mon_thousands_sep'];
|
|
|
587 |
|
|
|
588 |
if (self::$thousandsSeparator == '') {
|
|
|
589 |
// Default to .
|
|
|
590 |
self::$thousandsSeparator = ',';
|
|
|
591 |
}
|
|
|
592 |
}
|
|
|
593 |
|
|
|
594 |
return self::$thousandsSeparator;
|
|
|
595 |
}
|
|
|
596 |
|
|
|
597 |
/**
|
|
|
598 |
* Set the thousands separator. Only used by NumberFormat::toFormattedString()
|
|
|
599 |
* to format output by \PhpOffice\PhpSpreadsheet\Writer\Html and \PhpOffice\PhpSpreadsheet\Writer\Pdf.
|
|
|
600 |
*
|
|
|
601 |
* @param string $separator Character for thousands separator
|
|
|
602 |
*/
|
|
|
603 |
public static function setThousandsSeparator(string $separator): void
|
|
|
604 |
{
|
|
|
605 |
self::$thousandsSeparator = $separator;
|
|
|
606 |
}
|
|
|
607 |
|
|
|
608 |
/**
|
|
|
609 |
* Get the currency code. If it has not yet been set explicitly, try to obtain the
|
|
|
610 |
* symbol information from locale.
|
|
|
611 |
*/
|
|
|
612 |
public static function getCurrencyCode(): string
|
|
|
613 |
{
|
|
|
614 |
if (!empty(self::$currencyCode)) {
|
|
|
615 |
return self::$currencyCode;
|
|
|
616 |
}
|
|
|
617 |
self::$currencyCode = '$';
|
|
|
618 |
$localeconv = localeconv();
|
|
|
619 |
if (!empty($localeconv['currency_symbol'])) {
|
|
|
620 |
self::$currencyCode = $localeconv['currency_symbol'];
|
|
|
621 |
|
|
|
622 |
return self::$currencyCode;
|
|
|
623 |
}
|
|
|
624 |
if (!empty($localeconv['int_curr_symbol'])) {
|
|
|
625 |
self::$currencyCode = $localeconv['int_curr_symbol'];
|
|
|
626 |
|
|
|
627 |
return self::$currencyCode;
|
|
|
628 |
}
|
|
|
629 |
|
|
|
630 |
return self::$currencyCode;
|
|
|
631 |
}
|
|
|
632 |
|
|
|
633 |
/**
|
|
|
634 |
* Set the currency code. Only used by NumberFormat::toFormattedString()
|
|
|
635 |
* to format output by \PhpOffice\PhpSpreadsheet\Writer\Html and \PhpOffice\PhpSpreadsheet\Writer\Pdf.
|
|
|
636 |
*
|
|
|
637 |
* @param string $currencyCode Character for currency code
|
|
|
638 |
*/
|
|
|
639 |
public static function setCurrencyCode(string $currencyCode): void
|
|
|
640 |
{
|
|
|
641 |
self::$currencyCode = $currencyCode;
|
|
|
642 |
}
|
|
|
643 |
|
|
|
644 |
/**
|
|
|
645 |
* Convert SYLK encoded string to UTF-8.
|
|
|
646 |
*
|
|
|
647 |
* @param string $textValue SYLK encoded string
|
|
|
648 |
*
|
|
|
649 |
* @return string UTF-8 encoded string
|
|
|
650 |
*/
|
|
|
651 |
public static function SYLKtoUTF8(string $textValue): string
|
|
|
652 |
{
|
|
|
653 |
self::buildCharacterSets();
|
|
|
654 |
|
|
|
655 |
// If there is no escape character in the string there is nothing to do
|
|
|
656 |
if (strpos($textValue, '') === false) {
|
|
|
657 |
return $textValue;
|
|
|
658 |
}
|
|
|
659 |
|
|
|
660 |
foreach (self::$SYLKCharacters as $k => $v) {
|
|
|
661 |
$textValue = str_replace($k, $v, $textValue);
|
|
|
662 |
}
|
|
|
663 |
|
|
|
664 |
return $textValue;
|
|
|
665 |
}
|
|
|
666 |
|
|
|
667 |
/**
|
|
|
668 |
* Retrieve any leading numeric part of a string, or return the full string if no leading numeric
|
|
|
669 |
* (handles basic integer or float, but not exponent or non decimal).
|
|
|
670 |
*
|
|
|
671 |
* @param string $textValue
|
|
|
672 |
*
|
|
|
673 |
* @return mixed string or only the leading numeric part of the string
|
|
|
674 |
*/
|
|
|
675 |
public static function testStringAsNumeric($textValue)
|
|
|
676 |
{
|
|
|
677 |
if (is_numeric($textValue)) {
|
|
|
678 |
return $textValue;
|
|
|
679 |
}
|
|
|
680 |
$v = (float) $textValue;
|
|
|
681 |
|
|
|
682 |
return (is_numeric(substr($textValue, 0, strlen((string) $v)))) ? $v : $textValue;
|
|
|
683 |
}
|
|
|
684 |
}
|