1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Shared; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException; |
6
|
|
|
use Stringable; |
7
|
|
|
|
8
|
|
|
class StringHelper |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Control characters array. |
12
|
|
|
* |
13
|
|
|
* @var string[] |
14
|
|
|
*/ |
15
|
|
|
private static array $controlCharacters = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* SYLK Characters array. |
19
|
|
|
*/ |
20
|
|
|
private static array $SYLKCharacters = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Decimal separator. |
24
|
|
|
*/ |
25
|
|
|
private static ?string $decimalSeparator = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Thousands separator. |
29
|
|
|
*/ |
30
|
|
|
private static ?string $thousandsSeparator = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Currency code. |
34
|
|
|
*/ |
35
|
|
|
private static ?string $currencyCode = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Is iconv extension avalable? |
39
|
|
|
*/ |
40
|
|
|
private static ?bool $isIconvEnabled = null; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* iconv options. |
44
|
|
|
*/ |
45
|
|
|
private static string $iconvOptions = '//IGNORE//TRANSLIT'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Build control characters array. |
49
|
|
|
*/ |
50
|
107 |
|
private static function buildControlCharacters(): void |
51
|
|
|
{ |
52
|
107 |
|
for ($i = 0; $i <= 31; ++$i) { |
53
|
107 |
|
if ($i != 9 && $i != 10 && $i != 13) { |
54
|
107 |
|
$find = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_'; |
55
|
107 |
|
$replace = chr($i); |
56
|
107 |
|
self::$controlCharacters[$find] = $replace; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Build SYLK characters array. |
63
|
|
|
*/ |
64
|
107 |
|
private static function buildSYLKCharacters(): void |
65
|
|
|
{ |
66
|
107 |
|
self::$SYLKCharacters = [ |
67
|
107 |
|
"\x1B 0" => chr(0), |
68
|
107 |
|
"\x1B 1" => chr(1), |
69
|
107 |
|
"\x1B 2" => chr(2), |
70
|
107 |
|
"\x1B 3" => chr(3), |
71
|
107 |
|
"\x1B 4" => chr(4), |
72
|
107 |
|
"\x1B 5" => chr(5), |
73
|
107 |
|
"\x1B 6" => chr(6), |
74
|
107 |
|
"\x1B 7" => chr(7), |
75
|
107 |
|
"\x1B 8" => chr(8), |
76
|
107 |
|
"\x1B 9" => chr(9), |
77
|
107 |
|
"\x1B :" => chr(10), |
78
|
107 |
|
"\x1B ;" => chr(11), |
79
|
107 |
|
"\x1B <" => chr(12), |
80
|
107 |
|
"\x1B =" => chr(13), |
81
|
107 |
|
"\x1B >" => chr(14), |
82
|
107 |
|
"\x1B ?" => chr(15), |
83
|
107 |
|
"\x1B!0" => chr(16), |
84
|
107 |
|
"\x1B!1" => chr(17), |
85
|
107 |
|
"\x1B!2" => chr(18), |
86
|
107 |
|
"\x1B!3" => chr(19), |
87
|
107 |
|
"\x1B!4" => chr(20), |
88
|
107 |
|
"\x1B!5" => chr(21), |
89
|
107 |
|
"\x1B!6" => chr(22), |
90
|
107 |
|
"\x1B!7" => chr(23), |
91
|
107 |
|
"\x1B!8" => chr(24), |
92
|
107 |
|
"\x1B!9" => chr(25), |
93
|
107 |
|
"\x1B!:" => chr(26), |
94
|
107 |
|
"\x1B!;" => chr(27), |
95
|
107 |
|
"\x1B!<" => chr(28), |
96
|
107 |
|
"\x1B!=" => chr(29), |
97
|
107 |
|
"\x1B!>" => chr(30), |
98
|
107 |
|
"\x1B!?" => chr(31), |
99
|
107 |
|
"\x1B'?" => chr(127), |
100
|
107 |
|
"\x1B(0" => '€', // 128 in CP1252 |
101
|
107 |
|
"\x1B(2" => '‚', // 130 in CP1252 |
102
|
107 |
|
"\x1B(3" => 'ƒ', // 131 in CP1252 |
103
|
107 |
|
"\x1B(4" => '„', // 132 in CP1252 |
104
|
107 |
|
"\x1B(5" => '…', // 133 in CP1252 |
105
|
107 |
|
"\x1B(6" => '†', // 134 in CP1252 |
106
|
107 |
|
"\x1B(7" => '‡', // 135 in CP1252 |
107
|
107 |
|
"\x1B(8" => 'ˆ', // 136 in CP1252 |
108
|
107 |
|
"\x1B(9" => '‰', // 137 in CP1252 |
109
|
107 |
|
"\x1B(:" => 'Š', // 138 in CP1252 |
110
|
107 |
|
"\x1B(;" => '‹', // 139 in CP1252 |
111
|
107 |
|
"\x1BNj" => 'Œ', // 140 in CP1252 |
112
|
107 |
|
"\x1B(>" => 'Ž', // 142 in CP1252 |
113
|
107 |
|
"\x1B)1" => '‘', // 145 in CP1252 |
114
|
107 |
|
"\x1B)2" => '’', // 146 in CP1252 |
115
|
107 |
|
"\x1B)3" => '“', // 147 in CP1252 |
116
|
107 |
|
"\x1B)4" => '”', // 148 in CP1252 |
117
|
107 |
|
"\x1B)5" => '•', // 149 in CP1252 |
118
|
107 |
|
"\x1B)6" => '–', // 150 in CP1252 |
119
|
107 |
|
"\x1B)7" => '—', // 151 in CP1252 |
120
|
107 |
|
"\x1B)8" => '˜', // 152 in CP1252 |
121
|
107 |
|
"\x1B)9" => '™', // 153 in CP1252 |
122
|
107 |
|
"\x1B):" => 'š', // 154 in CP1252 |
123
|
107 |
|
"\x1B);" => '›', // 155 in CP1252 |
124
|
107 |
|
"\x1BNz" => 'œ', // 156 in CP1252 |
125
|
107 |
|
"\x1B)>" => 'ž', // 158 in CP1252 |
126
|
107 |
|
"\x1B)?" => 'Ÿ', // 159 in CP1252 |
127
|
107 |
|
"\x1B*0" => ' ', // 160 in CP1252 |
128
|
107 |
|
"\x1BN!" => '¡', // 161 in CP1252 |
129
|
107 |
|
"\x1BN\"" => '¢', // 162 in CP1252 |
130
|
107 |
|
"\x1BN#" => '£', // 163 in CP1252 |
131
|
107 |
|
"\x1BN(" => '¤', // 164 in CP1252 |
132
|
107 |
|
"\x1BN%" => '¥', // 165 in CP1252 |
133
|
107 |
|
"\x1B*6" => '¦', // 166 in CP1252 |
134
|
107 |
|
"\x1BN'" => '§', // 167 in CP1252 |
135
|
107 |
|
"\x1BNH " => '¨', // 168 in CP1252 |
136
|
107 |
|
"\x1BNS" => '©', // 169 in CP1252 |
137
|
107 |
|
"\x1BNc" => 'ª', // 170 in CP1252 |
138
|
107 |
|
"\x1BN+" => '«', // 171 in CP1252 |
139
|
107 |
|
"\x1B*<" => '¬', // 172 in CP1252 |
140
|
107 |
|
"\x1B*=" => '', // 173 in CP1252 |
141
|
107 |
|
"\x1BNR" => '®', // 174 in CP1252 |
142
|
107 |
|
"\x1B*?" => '¯', // 175 in CP1252 |
143
|
107 |
|
"\x1BN0" => '°', // 176 in CP1252 |
144
|
107 |
|
"\x1BN1" => '±', // 177 in CP1252 |
145
|
107 |
|
"\x1BN2" => '²', // 178 in CP1252 |
146
|
107 |
|
"\x1BN3" => '³', // 179 in CP1252 |
147
|
107 |
|
"\x1BNB " => '´', // 180 in CP1252 |
148
|
107 |
|
"\x1BN5" => 'µ', // 181 in CP1252 |
149
|
107 |
|
"\x1BN6" => '¶', // 182 in CP1252 |
150
|
107 |
|
"\x1BN7" => '·', // 183 in CP1252 |
151
|
107 |
|
"\x1B+8" => '¸', // 184 in CP1252 |
152
|
107 |
|
"\x1BNQ" => '¹', // 185 in CP1252 |
153
|
107 |
|
"\x1BNk" => 'º', // 186 in CP1252 |
154
|
107 |
|
"\x1BN;" => '»', // 187 in CP1252 |
155
|
107 |
|
"\x1BN<" => '¼', // 188 in CP1252 |
156
|
107 |
|
"\x1BN=" => '½', // 189 in CP1252 |
157
|
107 |
|
"\x1BN>" => '¾', // 190 in CP1252 |
158
|
107 |
|
"\x1BN?" => '¿', // 191 in CP1252 |
159
|
107 |
|
"\x1BNAA" => 'À', // 192 in CP1252 |
160
|
107 |
|
"\x1BNBA" => 'Á', // 193 in CP1252 |
161
|
107 |
|
"\x1BNCA" => 'Â', // 194 in CP1252 |
162
|
107 |
|
"\x1BNDA" => 'Ã', // 195 in CP1252 |
163
|
107 |
|
"\x1BNHA" => 'Ä', // 196 in CP1252 |
164
|
107 |
|
"\x1BNJA" => 'Å', // 197 in CP1252 |
165
|
107 |
|
"\x1BNa" => 'Æ', // 198 in CP1252 |
166
|
107 |
|
"\x1BNKC" => 'Ç', // 199 in CP1252 |
167
|
107 |
|
"\x1BNAE" => 'È', // 200 in CP1252 |
168
|
107 |
|
"\x1BNBE" => 'É', // 201 in CP1252 |
169
|
107 |
|
"\x1BNCE" => 'Ê', // 202 in CP1252 |
170
|
107 |
|
"\x1BNHE" => 'Ë', // 203 in CP1252 |
171
|
107 |
|
"\x1BNAI" => 'Ì', // 204 in CP1252 |
172
|
107 |
|
"\x1BNBI" => 'Í', // 205 in CP1252 |
173
|
107 |
|
"\x1BNCI" => 'Î', // 206 in CP1252 |
174
|
107 |
|
"\x1BNHI" => 'Ï', // 207 in CP1252 |
175
|
107 |
|
"\x1BNb" => 'Ð', // 208 in CP1252 |
176
|
107 |
|
"\x1BNDN" => 'Ñ', // 209 in CP1252 |
177
|
107 |
|
"\x1BNAO" => 'Ò', // 210 in CP1252 |
178
|
107 |
|
"\x1BNBO" => 'Ó', // 211 in CP1252 |
179
|
107 |
|
"\x1BNCO" => 'Ô', // 212 in CP1252 |
180
|
107 |
|
"\x1BNDO" => 'Õ', // 213 in CP1252 |
181
|
107 |
|
"\x1BNHO" => 'Ö', // 214 in CP1252 |
182
|
107 |
|
"\x1B-7" => '×', // 215 in CP1252 |
183
|
107 |
|
"\x1BNi" => 'Ø', // 216 in CP1252 |
184
|
107 |
|
"\x1BNAU" => 'Ù', // 217 in CP1252 |
185
|
107 |
|
"\x1BNBU" => 'Ú', // 218 in CP1252 |
186
|
107 |
|
"\x1BNCU" => 'Û', // 219 in CP1252 |
187
|
107 |
|
"\x1BNHU" => 'Ü', // 220 in CP1252 |
188
|
107 |
|
"\x1B-=" => 'Ý', // 221 in CP1252 |
189
|
107 |
|
"\x1BNl" => 'Þ', // 222 in CP1252 |
190
|
107 |
|
"\x1BN{" => 'ß', // 223 in CP1252 |
191
|
107 |
|
"\x1BNAa" => 'à', // 224 in CP1252 |
192
|
107 |
|
"\x1BNBa" => 'á', // 225 in CP1252 |
193
|
107 |
|
"\x1BNCa" => 'â', // 226 in CP1252 |
194
|
107 |
|
"\x1BNDa" => 'ã', // 227 in CP1252 |
195
|
107 |
|
"\x1BNHa" => 'ä', // 228 in CP1252 |
196
|
107 |
|
"\x1BNJa" => 'å', // 229 in CP1252 |
197
|
107 |
|
"\x1BNq" => 'æ', // 230 in CP1252 |
198
|
107 |
|
"\x1BNKc" => 'ç', // 231 in CP1252 |
199
|
107 |
|
"\x1BNAe" => 'è', // 232 in CP1252 |
200
|
107 |
|
"\x1BNBe" => 'é', // 233 in CP1252 |
201
|
107 |
|
"\x1BNCe" => 'ê', // 234 in CP1252 |
202
|
107 |
|
"\x1BNHe" => 'ë', // 235 in CP1252 |
203
|
107 |
|
"\x1BNAi" => 'ì', // 236 in CP1252 |
204
|
107 |
|
"\x1BNBi" => 'í', // 237 in CP1252 |
205
|
107 |
|
"\x1BNCi" => 'î', // 238 in CP1252 |
206
|
107 |
|
"\x1BNHi" => 'ï', // 239 in CP1252 |
207
|
107 |
|
"\x1BNs" => 'ð', // 240 in CP1252 |
208
|
107 |
|
"\x1BNDn" => 'ñ', // 241 in CP1252 |
209
|
107 |
|
"\x1BNAo" => 'ò', // 242 in CP1252 |
210
|
107 |
|
"\x1BNBo" => 'ó', // 243 in CP1252 |
211
|
107 |
|
"\x1BNCo" => 'ô', // 244 in CP1252 |
212
|
107 |
|
"\x1BNDo" => 'õ', // 245 in CP1252 |
213
|
107 |
|
"\x1BNHo" => 'ö', // 246 in CP1252 |
214
|
107 |
|
"\x1B/7" => '÷', // 247 in CP1252 |
215
|
107 |
|
"\x1BNy" => 'ø', // 248 in CP1252 |
216
|
107 |
|
"\x1BNAu" => 'ù', // 249 in CP1252 |
217
|
107 |
|
"\x1BNBu" => 'ú', // 250 in CP1252 |
218
|
107 |
|
"\x1BNCu" => 'û', // 251 in CP1252 |
219
|
107 |
|
"\x1BNHu" => 'ü', // 252 in CP1252 |
220
|
107 |
|
"\x1B/=" => 'ý', // 253 in CP1252 |
221
|
107 |
|
"\x1BN|" => 'þ', // 254 in CP1252 |
222
|
107 |
|
"\x1BNHy" => 'ÿ', // 255 in CP1252 |
223
|
107 |
|
]; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Get whether iconv extension is available. |
228
|
|
|
*/ |
229
|
223 |
|
public static function getIsIconvEnabled(): bool |
230
|
|
|
{ |
231
|
223 |
|
if (isset(self::$isIconvEnabled)) { |
232
|
223 |
|
return self::$isIconvEnabled; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
// Assume no problems with iconv |
236
|
75 |
|
self::$isIconvEnabled = true; |
237
|
|
|
|
238
|
|
|
// Fail if iconv doesn't exist |
239
|
75 |
|
if (!function_exists('iconv')) { |
240
|
|
|
self::$isIconvEnabled = false; |
241
|
75 |
|
} elseif (!@iconv('UTF-8', 'UTF-16LE', 'x')) { |
242
|
|
|
// Sometimes iconv is not working, and e.g. iconv('UTF-8', 'UTF-16LE', 'x') just returns false, |
243
|
|
|
self::$isIconvEnabled = false; |
244
|
75 |
|
} elseif (defined('PHP_OS') && @stristr(PHP_OS, 'AIX') && defined('ICONV_IMPL') && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && defined('ICONV_VERSION') && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) { |
245
|
|
|
// CUSTOM: IBM AIX iconv() does not work |
246
|
|
|
self::$isIconvEnabled = false; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
// Deactivate iconv default options if they fail (as seen on IMB i) |
250
|
75 |
|
if (self::$isIconvEnabled && !@iconv('UTF-8', 'UTF-16LE' . self::$iconvOptions, 'x')) { |
251
|
|
|
self::$iconvOptions = ''; |
252
|
|
|
} |
253
|
|
|
|
254
|
75 |
|
return self::$isIconvEnabled; |
255
|
|
|
} |
256
|
|
|
|
257
|
623 |
|
private static function buildCharacterSets(): void |
258
|
|
|
{ |
259
|
623 |
|
if (empty(self::$controlCharacters)) { |
260
|
107 |
|
self::buildControlCharacters(); |
261
|
|
|
} |
262
|
|
|
|
263
|
623 |
|
if (empty(self::$SYLKCharacters)) { |
264
|
107 |
|
self::buildSYLKCharacters(); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Convert from OpenXML escaped control character to PHP control character. |
270
|
|
|
* |
271
|
|
|
* Excel 2007 team: |
272
|
|
|
* ---------------- |
273
|
|
|
* That's correct, control characters are stored directly in the shared-strings table. |
274
|
|
|
* We do encode characters that cannot be represented in XML using the following escape sequence: |
275
|
|
|
* _xHHHH_ where H represents a hexadecimal character in the character's value... |
276
|
|
|
* So you could end up with something like _x0008_ in a string (either in a cell value (<v>) |
277
|
|
|
* element or in the shared string <t> element. |
278
|
|
|
* |
279
|
|
|
* @param string $textValue Value to unescape |
280
|
|
|
*/ |
281
|
496 |
|
public static function controlCharacterOOXML2PHP(string $textValue): string |
282
|
|
|
{ |
283
|
496 |
|
self::buildCharacterSets(); |
284
|
|
|
|
285
|
496 |
|
return str_replace(array_keys(self::$controlCharacters), array_values(self::$controlCharacters), $textValue); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Convert from PHP control character to OpenXML escaped control character. |
290
|
|
|
* |
291
|
|
|
* Excel 2007 team: |
292
|
|
|
* ---------------- |
293
|
|
|
* That's correct, control characters are stored directly in the shared-strings table. |
294
|
|
|
* We do encode characters that cannot be represented in XML using the following escape sequence: |
295
|
|
|
* _xHHHH_ where H represents a hexadecimal character in the character's value... |
296
|
|
|
* So you could end up with something like _x0008_ in a string (either in a cell value (<v>) |
297
|
|
|
* element or in the shared string <t> element. |
298
|
|
|
* |
299
|
|
|
* @param string $textValue Value to escape |
300
|
|
|
*/ |
301
|
267 |
|
public static function controlCharacterPHP2OOXML(string $textValue): string |
302
|
|
|
{ |
303
|
267 |
|
self::buildCharacterSets(); |
304
|
|
|
|
305
|
267 |
|
return str_replace(array_values(self::$controlCharacters), array_keys(self::$controlCharacters), $textValue); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Try to sanitize UTF8, replacing invalid sequences with Unicode substitution characters. |
310
|
|
|
*/ |
311
|
9082 |
|
public static function sanitizeUTF8(string $textValue): string |
312
|
|
|
{ |
313
|
9082 |
|
$textValue = str_replace(["\xef\xbf\xbe", "\xef\xbf\xbf"], "\xef\xbf\xbd", $textValue); |
314
|
9082 |
|
$subst = mb_substitute_character(); // default is question mark |
315
|
9082 |
|
mb_substitute_character(65533); // Unicode substitution character |
316
|
|
|
// Phpstan does not think this can return false. |
317
|
9082 |
|
$returnValue = mb_convert_encoding($textValue, 'UTF-8', 'UTF-8'); |
318
|
9082 |
|
mb_substitute_character($subst); |
319
|
|
|
|
320
|
9082 |
|
return $returnValue; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Check if a string contains UTF8 data. |
325
|
|
|
*/ |
326
|
1 |
|
public static function isUTF8(string $textValue): bool |
327
|
|
|
{ |
328
|
1 |
|
return $textValue === self::sanitizeUTF8($textValue); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Formats a numeric value as a string for output in various output writers forcing |
333
|
|
|
* point as decimal separator in case locale is other than English. |
334
|
|
|
*/ |
335
|
937 |
|
public static function formatNumber(float|int|string|null $numericValue): string |
336
|
|
|
{ |
337
|
937 |
|
if (is_float($numericValue)) { |
338
|
937 |
|
return str_replace(',', '.', (string) $numericValue); |
339
|
|
|
} |
340
|
|
|
|
341
|
93 |
|
return (string) $numericValue; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Converts a UTF-8 string into BIFF8 Unicode string data (8-bit string length) |
346
|
|
|
* Writes the string using uncompressed notation, no rich text, no Asian phonetics |
347
|
|
|
* If mbstring extension is not available, ASCII is assumed, and compressed notation is used |
348
|
|
|
* although this will give wrong results for non-ASCII strings |
349
|
|
|
* see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3. |
350
|
|
|
* |
351
|
|
|
* @param string $textValue UTF-8 encoded string |
352
|
|
|
* @param array<int, array{strlen: int, fontidx: int}> $arrcRuns Details of rich text runs in $value |
353
|
|
|
*/ |
354
|
113 |
|
public static function UTF8toBIFF8UnicodeShort(string $textValue, array $arrcRuns = []): string |
355
|
|
|
{ |
356
|
|
|
// character count |
357
|
113 |
|
$ln = self::countCharacters($textValue, 'UTF-8'); |
358
|
|
|
// option flags |
359
|
113 |
|
if (empty($arrcRuns)) { |
360
|
113 |
|
$data = pack('CC', $ln, 0x0001); |
361
|
|
|
// characters |
362
|
113 |
|
$data .= self::convertEncoding($textValue, 'UTF-16LE', 'UTF-8'); |
363
|
|
|
} else { |
364
|
11 |
|
$data = pack('vC', $ln, 0x09); |
365
|
11 |
|
$data .= pack('v', count($arrcRuns)); |
366
|
|
|
// characters |
367
|
11 |
|
$data .= self::convertEncoding($textValue, 'UTF-16LE', 'UTF-8'); |
368
|
11 |
|
foreach ($arrcRuns as $cRun) { |
369
|
11 |
|
$data .= pack('v', $cRun['strlen']); |
370
|
11 |
|
$data .= pack('v', $cRun['fontidx']); |
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
|
374
|
113 |
|
return $data; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Converts a UTF-8 string into BIFF8 Unicode string data (16-bit string length) |
379
|
|
|
* Writes the string using uncompressed notation, no rich text, no Asian phonetics |
380
|
|
|
* If mbstring extension is not available, ASCII is assumed, and compressed notation is used |
381
|
|
|
* although this will give wrong results for non-ASCII strings |
382
|
|
|
* see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3. |
383
|
|
|
* |
384
|
|
|
* @param string $textValue UTF-8 encoded string |
385
|
|
|
*/ |
386
|
114 |
|
public static function UTF8toBIFF8UnicodeLong(string $textValue): string |
387
|
|
|
{ |
388
|
|
|
// characters |
389
|
114 |
|
$chars = self::convertEncoding($textValue, 'UTF-16LE', 'UTF-8'); |
390
|
114 |
|
$ln = (int) (strlen($chars) / 2); // N.B. - strlen, not mb_strlen issue #642 |
391
|
|
|
|
392
|
114 |
|
return pack('vC', $ln, 0x0001) . $chars; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Convert string from one encoding to another. |
397
|
|
|
* |
398
|
|
|
* @param string $to Encoding to convert to, e.g. 'UTF-8' |
399
|
|
|
* @param string $from Encoding to convert from, e.g. 'UTF-16LE' |
400
|
|
|
*/ |
401
|
222 |
|
public static function convertEncoding(string $textValue, string $to, string $from): string |
402
|
|
|
{ |
403
|
222 |
|
if (self::getIsIconvEnabled()) { |
404
|
222 |
|
$result = iconv($from, $to . self::$iconvOptions, $textValue); |
405
|
222 |
|
if (false !== $result) { |
406
|
222 |
|
return $result; |
407
|
|
|
} |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
return mb_convert_encoding($textValue, $to, $from); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* Get character count. |
415
|
|
|
* |
416
|
|
|
* @param string $encoding Encoding |
417
|
|
|
* |
418
|
|
|
* @return int Character count |
419
|
|
|
*/ |
420
|
10545 |
|
public static function countCharacters(string $textValue, string $encoding = 'UTF-8'): int |
421
|
|
|
{ |
422
|
10545 |
|
return mb_strlen($textValue, $encoding); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* Get character count using mb_strwidth rather than mb_strlen. |
427
|
|
|
* |
428
|
|
|
* @param string $encoding Encoding |
429
|
|
|
* |
430
|
|
|
* @return int Character count |
431
|
|
|
*/ |
432
|
75 |
|
public static function countCharactersDbcs(string $textValue, string $encoding = 'UTF-8'): int |
433
|
|
|
{ |
434
|
75 |
|
return mb_strwidth($textValue, $encoding); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* Get a substring of a UTF-8 encoded string. |
439
|
|
|
* |
440
|
|
|
* @param string $textValue UTF-8 encoded string |
441
|
|
|
* @param int $offset Start offset |
442
|
|
|
* @param ?int $length Maximum number of characters in substring |
443
|
|
|
*/ |
444
|
10516 |
|
public static function substring(string $textValue, int $offset, ?int $length = 0): string |
445
|
|
|
{ |
446
|
10516 |
|
return mb_substr($textValue, $offset, $length, 'UTF-8'); |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* Convert a UTF-8 encoded string to upper case. |
451
|
|
|
* |
452
|
|
|
* @param string $textValue UTF-8 encoded string |
453
|
|
|
*/ |
454
|
10327 |
|
public static function strToUpper(string $textValue): string |
455
|
|
|
{ |
456
|
10327 |
|
return mb_convert_case($textValue, MB_CASE_UPPER, 'UTF-8'); |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* Convert a UTF-8 encoded string to lower case. |
461
|
|
|
* |
462
|
|
|
* @param string $textValue UTF-8 encoded string |
463
|
|
|
*/ |
464
|
10075 |
|
public static function strToLower(string $textValue): string |
465
|
|
|
{ |
466
|
10075 |
|
return mb_convert_case($textValue, MB_CASE_LOWER, 'UTF-8'); |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* Convert a UTF-8 encoded string to title/proper case |
471
|
|
|
* (uppercase every first character in each word, lower case all other characters). |
472
|
|
|
* |
473
|
|
|
* @param string $textValue UTF-8 encoded string |
474
|
|
|
*/ |
475
|
18 |
|
public static function strToTitle(string $textValue): string |
476
|
|
|
{ |
477
|
18 |
|
return mb_convert_case($textValue, MB_CASE_TITLE, 'UTF-8'); |
478
|
|
|
} |
479
|
|
|
|
480
|
21 |
|
public static function mbIsUpper(string $character): bool |
481
|
|
|
{ |
482
|
21 |
|
return mb_strtolower($character, 'UTF-8') !== $character; |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* Splits a UTF-8 string into an array of individual characters. |
487
|
|
|
*/ |
488
|
21 |
|
public static function mbStrSplit(string $string): array |
489
|
|
|
{ |
490
|
|
|
// Split at all position not after the start: ^ |
491
|
|
|
// and not before the end: $ |
492
|
21 |
|
$split = preg_split('/(?<!^)(?!$)/u', $string); |
493
|
|
|
|
494
|
21 |
|
return ($split === false) ? [] : $split; |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
/** |
498
|
|
|
* Reverse the case of a string, so that all uppercase characters become lowercase |
499
|
|
|
* and all lowercase characters become uppercase. |
500
|
|
|
* |
501
|
|
|
* @param string $textValue UTF-8 encoded string |
502
|
|
|
*/ |
503
|
21 |
|
public static function strCaseReverse(string $textValue): string |
504
|
|
|
{ |
505
|
21 |
|
$characters = self::mbStrSplit($textValue); |
506
|
21 |
|
foreach ($characters as &$character) { |
507
|
21 |
|
if (self::mbIsUpper($character)) { |
508
|
14 |
|
$character = mb_strtolower($character, 'UTF-8'); |
509
|
|
|
} else { |
510
|
17 |
|
$character = mb_strtoupper($character, 'UTF-8'); |
511
|
|
|
} |
512
|
|
|
} |
513
|
|
|
|
514
|
21 |
|
return implode('', $characters); |
515
|
|
|
} |
516
|
|
|
|
517
|
1 |
|
private static function useAlt(string $altValue, string $default, bool $trimAlt): string |
518
|
|
|
{ |
519
|
1 |
|
return ($trimAlt ? trim($altValue) : $altValue) ?: $default; |
520
|
|
|
} |
521
|
|
|
|
522
|
140 |
|
private static function getLocaleValue(string $key, string $altKey, string $default, bool $trimAlt = false): string |
523
|
|
|
{ |
524
|
140 |
|
$localeconv = localeconv(); |
525
|
140 |
|
$rslt = $localeconv[$key]; |
526
|
|
|
// win-1252 implements Euro as 0x80 plus other symbols |
527
|
140 |
|
if (preg_match('//u', $rslt) !== 1) { |
528
|
1 |
|
$rslt = ''; |
529
|
|
|
} |
530
|
|
|
|
531
|
140 |
|
return $rslt ?: self::useAlt($localeconv[$altKey], $default, $trimAlt); |
532
|
|
|
} |
533
|
|
|
|
534
|
|
|
/** |
535
|
|
|
* Get the decimal separator. If it has not yet been set explicitly, try to obtain number |
536
|
|
|
* formatting information from locale. |
537
|
|
|
*/ |
538
|
1103 |
|
public static function getDecimalSeparator(): string |
539
|
|
|
{ |
540
|
1103 |
|
if (!isset(self::$decimalSeparator)) { |
541
|
128 |
|
self::$decimalSeparator = self::getLocaleValue('decimal_point', 'mon_decimal_point', '.'); |
542
|
|
|
} |
543
|
|
|
|
544
|
1103 |
|
return self::$decimalSeparator; |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
/** |
548
|
|
|
* Set the decimal separator. Only used by NumberFormat::toFormattedString() |
549
|
|
|
* to format output by \PhpOffice\PhpSpreadsheet\Writer\Html and \PhpOffice\PhpSpreadsheet\Writer\Pdf. |
550
|
|
|
* |
551
|
|
|
* @param ?string $separator Character for decimal separator |
552
|
|
|
*/ |
553
|
926 |
|
public static function setDecimalSeparator(?string $separator): void |
554
|
|
|
{ |
555
|
926 |
|
self::$decimalSeparator = $separator; |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
/** |
559
|
|
|
* Get the thousands separator. If it has not yet been set explicitly, try to obtain number |
560
|
|
|
* formatting information from locale. |
561
|
|
|
*/ |
562
|
1115 |
|
public static function getThousandsSeparator(): string |
563
|
|
|
{ |
564
|
1115 |
|
if (!isset(self::$thousandsSeparator)) { |
565
|
129 |
|
self::$thousandsSeparator = self::getLocaleValue('thousands_sep', 'mon_thousands_sep', ','); |
566
|
|
|
} |
567
|
|
|
|
568
|
1115 |
|
return self::$thousandsSeparator; |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
/** |
572
|
|
|
* Set the thousands separator. Only used by NumberFormat::toFormattedString() |
573
|
|
|
* to format output by \PhpOffice\PhpSpreadsheet\Writer\Html and \PhpOffice\PhpSpreadsheet\Writer\Pdf. |
574
|
|
|
* |
575
|
|
|
* @param ?string $separator Character for thousands separator |
576
|
|
|
*/ |
577
|
926 |
|
public static function setThousandsSeparator(?string $separator): void |
578
|
|
|
{ |
579
|
926 |
|
self::$thousandsSeparator = $separator; |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
/** |
583
|
|
|
* Get the currency code. If it has not yet been set explicitly, try to obtain the |
584
|
|
|
* symbol information from locale. |
585
|
|
|
*/ |
586
|
78 |
|
public static function getCurrencyCode(bool $trimAlt = false): string |
587
|
|
|
{ |
588
|
78 |
|
if (!isset(self::$currencyCode)) { |
589
|
28 |
|
self::$currencyCode = self::getLocaleValue('currency_symbol', 'int_curr_symbol', '$', $trimAlt); |
590
|
|
|
} |
591
|
|
|
|
592
|
78 |
|
return self::$currencyCode; |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
/** |
596
|
|
|
* Set the currency code. Only used by NumberFormat::toFormattedString() |
597
|
|
|
* to format output by \PhpOffice\PhpSpreadsheet\Writer\Html and \PhpOffice\PhpSpreadsheet\Writer\Pdf. |
598
|
|
|
* |
599
|
|
|
* @param ?string $currencyCode Character for currency code |
600
|
|
|
*/ |
601
|
924 |
|
public static function setCurrencyCode(?string $currencyCode): void |
602
|
|
|
{ |
603
|
924 |
|
self::$currencyCode = $currencyCode; |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
/** |
607
|
|
|
* Convert SYLK encoded string to UTF-8. |
608
|
|
|
* |
609
|
|
|
* @param string $textValue SYLK encoded string |
610
|
|
|
* |
611
|
|
|
* @return string UTF-8 encoded string |
612
|
|
|
*/ |
613
|
11 |
|
public static function SYLKtoUTF8(string $textValue): string |
614
|
|
|
{ |
615
|
11 |
|
self::buildCharacterSets(); |
616
|
|
|
|
617
|
|
|
// If there is no escape character in the string there is nothing to do |
618
|
11 |
|
if (!str_contains($textValue, '')) { |
619
|
10 |
|
return $textValue; |
620
|
|
|
} |
621
|
|
|
|
622
|
3 |
|
foreach (self::$SYLKCharacters as $k => $v) { |
623
|
3 |
|
$textValue = str_replace($k, $v, $textValue); |
624
|
|
|
} |
625
|
|
|
|
626
|
3 |
|
return $textValue; |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
/** |
630
|
|
|
* Retrieve any leading numeric part of a string, or return the full string if no leading numeric |
631
|
|
|
* (handles basic integer or float, but not exponent or non decimal). |
632
|
|
|
* |
633
|
|
|
* @return float|string string or only the leading numeric part of the string |
634
|
|
|
*/ |
635
|
284 |
|
public static function testStringAsNumeric(string $textValue): float|string |
636
|
|
|
{ |
637
|
284 |
|
if (is_numeric($textValue)) { |
638
|
281 |
|
return $textValue; |
639
|
|
|
} |
640
|
9 |
|
$v = (float) $textValue; |
641
|
|
|
|
642
|
9 |
|
return (is_numeric(substr($textValue, 0, strlen((string) $v)))) ? $v : $textValue; |
643
|
|
|
} |
644
|
|
|
|
645
|
37 |
|
public static function strlenAllowNull(?string $string): int |
646
|
|
|
{ |
647
|
37 |
|
return strlen("$string"); |
648
|
|
|
} |
649
|
|
|
|
650
|
10108 |
|
public static function convertToString(mixed $value, bool $throw = true, string $default = ''): string |
651
|
|
|
{ |
652
|
10108 |
|
if ($value === null || is_scalar($value) || $value instanceof Stringable) { |
653
|
10104 |
|
return (string) $value; |
654
|
|
|
} |
655
|
|
|
|
656
|
5 |
|
if ($throw) { |
657
|
5 |
|
throw new SpreadsheetException('Unable to convert to string'); |
658
|
|
|
} |
659
|
|
|
|
660
|
1 |
|
return $default; |
661
|
|
|
} |
662
|
|
|
|
663
|
|
|
/** |
664
|
|
|
* Assist with POST items when samples are run in browser. |
665
|
|
|
* Never run as part of unit tests, which are command line. |
666
|
|
|
* |
667
|
|
|
* @codeCoverageIgnore |
668
|
|
|
*/ |
669
|
|
|
public static function convertPostToString(string $index, string $default = ''): string |
670
|
|
|
{ |
671
|
|
|
if (isset($_POST[$index])) { |
672
|
|
|
return htmlentities(self::convertToString($_POST[$index], false, $default)); |
673
|
|
|
} |
674
|
|
|
|
675
|
|
|
return $default; |
676
|
|
|
} |
677
|
|
|
} |
678
|
|
|
|