From cd825621e83e9060b052ef93155d923a5c4e67e5 Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Fri, 13 Nov 2026 01:18:16 +0100 Subject: [PATCH] Fall back to mbstring in StringHelper::transcode() without ext-iconv transcode() reads the ICONV_IMPL constant and calls iconv(), but composer.json declares no dependency on ext-iconv. Without the extension it threw "Undefined constant ICONV_IMPL". strcmp() and strcasecmp() call transcode() for non-UTF-8 locales. Use mb_convert_encoding() when iconv is not available, and suggest ext-iconv for transliteration. Co-Authored-By: Claude Opus 5.5 --- composer.json | 1 + docs/overview.md | 14 ++++++++++++++ src/StringHelper.php | 7 +++++++ 3 files changed, 22 insertions(+) diff --git a/composer.json b/composer.json index 5ddf279e..9493ef39 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "phpstan/phpstan-deprecation-rules": "^2.0.3" }, "suggest": { + "ext-iconv": "To transcode strings with transliteration in StringHelper::transcode()", "ext-mbstring": "For improved processing", "doctrine/inflector": "To use the string inflector" }, diff --git a/docs/overview.md b/docs/overview.md index 5c220497..74f4790d 100644 --- a/docs/overview.md +++ b/docs/overview.md @@ -1,3 +1,17 @@ # String package TODO + +## `StringHelper::transcode()` + +`StringHelper::transcode($source, $fromEncoding, $toEncoding)` converts a string between character +sets. With `ext-iconv` it uses `iconv()` with transliteration, so characters the target encoding +cannot represent are approximated or dropped (`€` becomes `EUR` in ISO-8859-1). `ext-iconv` is +suggested, not required. Without it, `transcode()` uses `mb_convert_encoding()`, which replaces +such characters with `?`. + +```php +use Joomla\String\StringHelper; + +StringHelper::transcode('Åbc €100', 'UTF-8', 'ISO-8859-1'); // "\xC5bc EUR100" with ext-iconv, "\xC5bc ?100" without +``` diff --git a/src/StringHelper.php b/src/StringHelper.php index 98f73e7d..5a4ccdac 100644 --- a/src/StringHelper.php +++ b/src/StringHelper.php @@ -724,6 +724,9 @@ public static function ucwords($str) /** * Transcode a string. * + * Uses iconv() with transliteration when the iconv extension is available. Without it, the string is converted with + * mb_convert_encoding(), which replaces characters that the target encoding cannot represent with "?". + * * @param string $source The string to transcode. * @param string $fromEncoding The source encoding. * @param string $toEncoding The target encoding. @@ -736,6 +739,10 @@ public static function ucwords($str) */ public static function transcode($source, $fromEncoding, $toEncoding) { + if (!\defined('ICONV_IMPL')) { + return mb_convert_encoding($source, $toEncoding, $fromEncoding); + } + switch (ICONV_IMPL) { case 'glibc': return @iconv($fromEncoding, $toEncoding . '//TRANSLIT,IGNORE', $source);