symfony/polyfill-mbstring
Native PHP polyfill for the mbstring extension, providing partial mb_* functionality when the mbstring extension isn’t available. Part of Symfony’s Polyfill suite for consistent multibyte string handling across environments.
The symfony/polyfill-mbstring package (v1.38.0) remains a highly compatible solution for Laravel applications requiring multibyte string support in environments lacking ext-mbstring. Key updates in v1.38.0 improve robustness while maintaining Laravel’s architectural alignment. Strengths persist:
mb_strlen()) and //IGNORE fallback for musl-based systems (e.g., Alpine Linux).spatie/laravel-translatable).Updated architectural trade-offs:
mb_http_output()), but v1.38.0’s mb_convert_encoding() fix for HTML-ENTITIES reduces HTML-related risks.//IGNORE fallback (bug #610) introduces behavioral changes for unsupported encodings (e.g., iconv may produce different outputs than native mbstring). Audit required for encoding-sensitive logic.ext-iconv (unchanged).musl-based systems (e.g., Alpine Docker) now handle //IGNORE gracefully but may alter encoding outputs.composer require symfony/polyfill-mbstring:^1.38
ext-iconv and test //IGNORE behavior in musl environments:
if (!extension_loaded('iconv')) {
throw new RuntimeException('ext-iconv is required.');
}
// Test encoding behavior in staging:
$test = mb_convert_encoding("€", "HTML-ENTITIES", "UTF-8");
assert($test === "€"); // Verify matches native mbstring
null inputs (bug #603) in mb_* functions (e.g., user-generated data).laravel-excel for mb_convert_encoding() changes.HTML-ENTITIES and //IGNORE outputs against native mbstring (if available).musl compatibility; may require apk add icu-libs for full Unicode support.| Risk Category | Specific Risk | Impact | Mitigation Strategy |
|---|---|---|---|
| Functional | //IGNORE fallback behavior |
Non-native encoding outputs (e.g., mb_convert_encoding() may differ from ext-mbstring). |
Test encoding-sensitive paths; enforce native mbstring where precision is critical. |
| Functional | Invalid UTF-8 input handling | mb_strlen() now returns false for invalid sequences (previously crashed). |
Validate UTF-8 input upstream (e.g., with mb_check_encoding() or iconv). |
| Environmental | musl/Alpine encoding quirks |
iconv fallback may alter behavior on non-glibc systems. |
Test in staging with Alpine Docker; document deviations. |
| Performance | Null input handling (bug #603) | Minor overhead for mb_* calls with null inputs. |
Benchmark; negligible for most use cases. |
| PHP Version | Dropped PHP 7.0–7.1 support | Breaking for legacy environments. | Upgrade to PHP 7.2+; use v1.34.0 if stuck on PHP 7.1. |
| Unicode | HTML-ENTITIES encoding fix |
mb_convert_encoding() now matches native output (reduces risk). |
Re-test HTML entities in Blade/validation. |
| Dependency | Third-party plugin conflicts | Packages assuming native mbstring may fail (e.g., laravel-excel). |
Update dependencies; test interactions in staging. |
| Maintenance | //IGNORE fallback complexity |
Future releases may refine encoding behavior. | Pin to ^1.38 for stability; monitor Symfony’s release notes. |
ext-iconv (unchanged).ext-mbstring (polyfill falls back; now with //IGNORE handling).musl/Alpine tests if used.apk add icu-libs for full Unicode support.ext-mbstring.Pre-migration:
mb_convert_encoding() calls (focus on HTML-ENTITIES).mb_strlen() on malformed input).ext-iconv and musl compatibility in staging.null inputs (bug #603).Integration:
composer.json:
composer require symfony/polyfill-mbstring:^1.38
bootstrap/app.php):
if (!extension_loaded('iconv')) {
throw new RuntimeException('ext-iconv is required.');
}
// Validate encoding behavior:
$htmlEntityTest = mb_convert_encoding("€", "HTML-ENTITIES", "UTF-8");
if ($htmlEntityTest !== "€") {
throw new RuntimeException('Encoding mismatch detected.');
}
Dockerfile:
RUN apk add icu-libs # Optional: for full Unicode support
Testing:
mb_strlen() with invalid UTF-8; test null inputs.mb_strlen() in form requests).Rollout:
mb_* usage behind a flag for gradual adoption.spatie/laravel-translatable, laravel-lang (v1.38.0’s fixes reduce risks).mb_convert_encoding() (e.g., laravel-excel) may need re-testing.mbstring).ext-iconv is mandatory; musl systems require additional testing.HTML-ENTITIES (now matches native).mb_strlen() no longer crashes).How can I help you explore Laravel packages today?