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.
mb_strlen() and mb_convert_encoding() handle edge cases without crashes.mb_convert_encoding($s, $y, 'HTML-ENTITIES') to match native mbstring behavior, critical for localized web content (e.g., converting é to é in multilingual blogs or admin panels).NullPointerException-like errors when passing null to mb_* functions, reducing runtime crashes in user-generated content workflows (e.g., comments, profiles).ext-mbstring is unavailable or misconfigured.mb_strlen() now handles invalid UTF-8 sequences (e.g., broken emoji) without failing.mb_convert_encoding improve compatibility with Japanese/Korean/Chinese text processing.php:7.4-alpine) where //IGNORE flag support is limited.é to é in blogs or admin panels).null to mb_* functions (e.g., in validation loops or dynamic content rendering).mb_strlen() crashes on invalid UTF-8 input (fixed in v1.38.0).ext-mbstring is enabled and properly configured (e.g., PHP 8.2+ with no musl/Alpine constraints).mbstring remains ~5–10x faster.mbstring functions (e.g., mb_http_output(), mb_detect_encoding(), or mb_convert_kana()).//IGNORE flag behavior for custom encoding logic (this polyfill now falls back to iconv for musl compatibility).ext-mbstring as a hard dependency (modern PHP versions include mbstring by default).*"This update to v1.38.0 eliminates critical crashes in our multilingual features—especially for emoji, CJK text, and user-generated content—by fixing edge cases like invalid UTF-8 input and null values in mb_* functions. For example:
mb_strlen() now handles corrupted text without failing.Ask: Which multilingual features are at risk from encoding errors or null inputs? How much revenue or user retention is at stake if these crashes persist?"
*"v1.38.0 fixes three critical bugs that improve stability for edge cases:
mb_strlen() no longer crashes on malformed input (e.g., broken emoji, corrupted CJK text).iconv when //IGNORE is unsupported, enabling deployments on lightweight Docker images.mb_* functions now accept null without throwing exceptions.Trade-offs:
//IGNORE support: The polyfill now uses iconv as a fallback for musl, which may behave differently for custom encoding logic.mbstring (acceptable for non-critical paths).Recommendation: Adopt for PHP 7.x/Alpine environments and audit code for:
mb_convert_encoding($s, $y, 'HTML-ENTITIES') usage (now matches native behavior).null inputs in mb_* functions (e.g., validation loops).//IGNORE logic (may need updates for musl compatibility)."*"v1.38.0 makes this polyfill even more reliable for edge cases. Key fixes:
mb_strlen() now handles invalid UTF-8 (e.g., broken emoji or corrupted text):
mb_strlen("\xFF\xFE"); // Returns 0 instead of crashing (PHP 7.x)
# Now compatible with:
php:7.4-alpine
php:8.0-alpine
null in mb_* functions:
mb_strlen(null); // Returns 0 (no more crashes)
How to use:
composer update symfony/polyfill-mbstring:^1.38
ext-iconv is installed (required for musl fallback):
docker-php-ext-install iconv
// Safe for malformed UTF-8
mb_strlen("\xFF\xFE"); // Works!
// Safe for null inputs
mb_substr(null, 0, 10); // Returns null
Warning: If you rely on //IGNORE flag behavior for custom encoding, test thoroughly—this polyfill now uses iconv as a fallback on musl."
*"This update directly supports our roadmap for globalization and user-generated content. Here’s how it enables key features:
| Feature | Blocked By | Solution | Outcome |
|---|---|---|---|
| Emoji support in chat | Crashes on malformed emoji sequences | mb_strlen() handles invalid UTF-8 |
Retain users with broken messages |
| CJK language packs | mb_strlen() fails on corrupted text |
Fixed in v1.38.0 | Launch Japanese/Korean forums on time |
| Alpine Linux deployments | //IGNORE unsupported on musl |
Fallback to iconv |
Reduce CI/CD infrastructure costs |
| HTML entity conversion | Inconsistent mb_convert_encoding() |
Now matches native behavior | Improve localized web content |
| Null-safe validation | Crashes on null inputs |
mb_* functions accept null |
Stabilize user-generated content |
Ask your team:
mb_* functions with user-generated or third-party input?How can I help you explore Laravel packages today?