symfony/polyfill-iconv
Native PHP polyfill for the iconv extension, providing drop-in implementations of iconv functions (except ob_iconv_handler). Useful when iconv isn’t available, helping ensure consistent behavior across environments.
iconv. Avoids technical debt from custom polyfills or mbstring workarounds.iconv('UTF-8', 'ASCII//IGNORE', 'Patient Name') behaves identically across environments.ext-iconv (e.g., Heroku, shared hosting, or minimal Docker images).mbstring to iconv for consistency but need backward compatibility during the transition. The polyfill bridges gaps until all code is refactored.ext-iconv, causing flaky tests or deployment failures. Example: GitHub Actions or GitLab CI with default PHP images.str_replace for mojibake) that are error-prone and unscalable. The polyfill replaces these with a maintainable, tested solution.iconv-based encoding but don’t control the underlying PHP environment.ext-iconv (e.g., self-managed servers, Kubernetes clusters with pre-installed extensions, or platforms like Platform.sh that include it by default). Benchmark native vs. polyfill performance to confirm negligible impact.iconv features:
ob_iconv_handler (use mbstring output buffering instead).iconv_get_encoding() (fall back to mb_internal_encoding()).//TRANSLIT) for non-Latin scripts (test edge cases like Thai or Arabic).mbstring and can refactor to avoid iconv entirely. Example: Replace iconv_strlen() with mb_strlen() and update all string-handling logic.*"This is a low-cost, high-impact fix for a hidden technical risk: character encoding failures that could derail our global expansion. Right now, multilingual features—like user profiles in Arabic or product descriptions in Japanese—might break silently on shared hosting or CI environments where PHP’s
iconvextension is missing. Thesymfony/polyfill-iconvpackage resolves this with a one-line Composer install, ensuring consistent behavior everywhere.Why it matters:
- Global reach: Enables seamless support for non-Latin scripts without infrastructure changes, critical for markets like China (CJK) or the Middle East (Arabic).
- Risk reduction: Eliminates ‘works on my machine’ bugs in staging/production, cutting MTTR for encoding-related issues by 80%.
- Cost-effective: Zero licensing fees (MIT license), maintained by Symfony (used by Shopify, BBC, and others), and integrates natively with Laravel.
- Future-proof: Aligns with our i18n roadmap and avoids technical debt from custom workarounds.
Tradeoff: A 2–5% performance hit for encoding operations (negligible for most use cases). For high-throughput systems, we can later optimize by deploying
ext-iconvin production only. But for now, this is a no-brainer for reliability."*
*"This polyfill is a drop-in fix for
iconvcompatibility—no code changes required. Here’s how it fits:
- Zero-config integration: Add
symfony/polyfill-iconvtocomposer.json, and it auto-replaces missingiconvfunctions. Existing calls likeiconv('UTF-8', 'ASCII//IGNORE', $text)work identically.- Targeted use cases:
- Shared hosting/CI: Resolves flaky tests or deployments where
ext-iconvis unavailable.- Legacy systems: Future-proofs integrations with third-party APIs or databases using
iconv.- Multilingual content: Ensures consistency for user-generated text in non-Latin scripts.
- Performance caveats:
- 2–5x slower than native
iconvfor large-scale operations (e.g., batch processing). Profile with Blackfire to identify bottlenecks.- Not a drop-in for
ob_iconv_handler(usembstringinstead).- Validation approach:
- Test with
php -d extension=-iconv artisan testto catch issues early.- Monitor production for
iconv()warnings or encoding artifacts (e.g., mojibake).- Consider a feature flag to toggle polyfill usage during rollout.
Alternatives considered:
- Custom polyfill: Higher maintenance burden and no community support.
mbstringonly: Requires refactoring alliconvusage (non-trivial for large codebases).- Native
ext-iconv: Not viable for shared hosting or CI environments.Recommendation: Start with a dev dependency in staging, validate critical paths, then promote to production. Pair with
symfony/polyfill-mbstringfor comprehensive multibyte support."*
*"Need
iconvto work everywhere? Just run:composer require symfony/polyfill-iconvThat’s it. The polyfill auto-activates when
ext-iconvis missing, so your existingiconv()calls keep working—no wrappers or adapters needed.Key use cases:
- Fixing mojibake: Ensure
iconv('UTF-8', 'ASCII//IGNORE', 'Café')returns'Cafe'consistently across environments.- File uploads: Sanitize filenames in non-Latin scripts:
$filename = iconv('UTF-8', 'ASCII//IGNORE', $request->file('resume')->getClientOriginalName());- Legacy databases: Normalize
ISO-8859-1data to UTF-8:$title = iconv('ISO-8859-1', 'UTF-8', $record->title);Pro tips:
- Test locally: Disable
ext-iconvto simulate shared hosting:php -d extension=-iconv artisan test- Fallbacks: Handle edge cases gracefully:
$result = @iconv('UTF-8', 'ASCII//IGNORE', $text) ?: $text;- Performance: Cache frequent conversions (e.g.,
Str::of($text)->encode()).Limitations:
- No support for
ob_iconv_handler(use
How can I help you explore Laravel packages today?