Skip to content

Commit 0492ea6

Browse files
authored
Use Only mb_convert_encoding in StringHelper sanitizeUTF8 (#2994)
* Test if UConverter Exists Without Autoload Fix #2982. That issue is actually closed, but it did expose a problem. Our test environments all enable php-intl, but that extension isn't a formal requirement for PhpSpreadsheet. Perhaps it ought to be. Nevertheless ... Using UConverter for string translation solved some problems for us. However, it is only available when php-intl is enabled. The code tests if it exists before using it, so no big deal ... except it seems likely that the people reporting the issue not only did not have php-intl, but they do have their own autoloader which issues an exception when the class isn't found. The test for existence of UConverter defaulted to attempting to autoload it if not found. So, on a system without php-intl but with a custom autoloader, there is a problem. Code is changed to suppress autoload when testing UConverter existence. Pending this fix, the workaround for this issue is to enable php-intl. * Minor Improvement Make mb_convert_encoding use same substitution character as UConverter, ensuring consistent results whatever the user's environment. * And Now That I Figured That Out Since mb_convert_encoding can now return the same output as UConverter, we don't need UConverter (or iconv) after all in sanitizeUTF8.
1 parent d13b07b commit 0492ea6

File tree

1 file changed

+3
-17
lines changed

1 file changed

+3
-17
lines changed

src/PhpSpreadsheet/Shared/StringHelper.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace PhpOffice\PhpSpreadsheet\Shared;
44

55
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6-
use UConverter;
76

87
class StringHelper
98
{
@@ -334,26 +333,13 @@ public static function controlCharacterPHP2OOXML($textValue)
334333
public static function sanitizeUTF8(string $textValue): string
335334
{
336335
$textValue = str_replace(["\xef\xbf\xbe", "\xef\xbf\xbf"], "\xef\xbf\xbd", $textValue);
337-
if (class_exists(UConverter::class)) {
338-
$returnValue = UConverter::transcode($textValue, 'UTF-8', 'UTF-8');
339-
if ($returnValue !== false) {
340-
return $returnValue;
341-
}
342-
}
343-
// @codeCoverageIgnoreStart
344-
// I don't think any of the code below should ever be executed.
345-
if (self::getIsIconvEnabled()) {
346-
$returnValue = @iconv('UTF-8', 'UTF-8', $textValue);
347-
if ($returnValue !== false) {
348-
return $returnValue;
349-
}
350-
}
351-
336+
$subst = mb_substitute_character(); // default is question mark
337+
mb_substitute_character(65533); // Unicode substitution character
352338
// Phpstan does not think this can return false.
353339
$returnValue = mb_convert_encoding($textValue, 'UTF-8', 'UTF-8');
340+
mb_substitute_character($subst);
354341

355342
return $returnValue;
356-
// @codeCoverageIgnoreEnd
357343
}
358344

359345
/**

0 commit comments

Comments
 (0)