Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Polyfill Iconv Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Globalization & Localization Expansion: Enables seamless handling of multilingual content (e.g., user-generated text, product descriptions in non-Latin scripts) without infrastructure constraints, directly supporting expansion into markets like China, Japan, or Arabic-speaking regions. Aligns with i18n feature flags or translation management systems (e.g., Laravel Localization).
  • Infrastructure Resilience: Mitigates "works on my machine" issues by ensuring consistent encoding behavior across shared hosting (e.g., GoDaddy, Hostinger), serverless (AWS Lambda), and CI/CD pipelines where PHP extensions are restricted. Reduces mean time to resolution (MTTR) for encoding-related bugs.
  • Legacy System Modernization: Justifies "buy" over "build" for encoding logic in monolithic apps or third-party integrations (e.g., legacy ERP systems, payment gateways) that rely on iconv. Avoids technical debt from custom polyfills or mbstring workarounds.
  • Performance vs. Reliability Tradeoffs: Provides a data-driven justification for prioritizing reliability over raw performance in non-critical paths (e.g., admin dashboards, static content). Example: Accept 3x slower encoding for 100% uptime in shared hosting vs. optimizing for 10% faster encoding in a dedicated VPS.
  • Compliance & Data Integrity: Ensures accurate character handling for regulated industries (e.g., healthcare, finance) where data corruption (e.g., mojibake in medical records) could violate standards like HIPAA or GDPR. Example: Polyfill guarantees iconv('UTF-8', 'ASCII//IGNORE', 'Patient Name') behaves identically across environments.

When to Consider This Package

Adopt if:

  • Your Laravel app must support multilingual content (e.g., user profiles, comments, or product catalogs in scripts like Arabic, Cyrillic, or CJK) but deploys to environments without ext-iconv (e.g., Heroku, shared hosting, or minimal Docker images).
  • You’re migrating from mbstring to iconv for consistency but need backward compatibility during the transition. The polyfill bridges gaps until all code is refactored.
  • Your CI/CD pipeline or staging environment lacks ext-iconv, causing flaky tests or deployment failures. Example: GitHub Actions or GitLab CI with default PHP images.
  • You’re auditing technical debt and find ad-hoc encoding fixes (e.g., regex hacks, str_replace for mojibake) that are error-prone and unscalable. The polyfill replaces these with a maintainable, tested solution.
  • Your Laravel app integrates with third-party systems (e.g., payment processors, APIs) that expect iconv-based encoding but don’t control the underlying PHP environment.

Avoid if:

  • Your entire infrastructure guarantees 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.
  • Performance is critical for encoding-heavy operations (e.g., processing >50k strings/sec in a high-frequency trading app or real-time translation API). Profile with tools like Blackfire to quantify overhead (typically 2–5x slower).
  • Your use case relies on unsupported iconv features:
    • ob_iconv_handler (use mbstring output buffering instead).
    • iconv_get_encoding() (fall back to mb_internal_encoding()).
    • Complex transliteration (//TRANSLIT) for non-Latin scripts (test edge cases like Thai or Arabic).
  • You’re exclusively using mbstring and can refactor to avoid iconv entirely. Example: Replace iconv_strlen() with mb_strlen() and update all string-handling logic.
  • Your team lacks PHP extension privileges but has the resources to maintain a custom polyfill (e.g., for proprietary encoding schemes). Assess whether the MIT-licensed Symfony polyfill meets your needs or if a bespoke solution is required.

How to Pitch It (Stakeholders)

For Executives (Business Impact)

*"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 iconv extension is missing. The symfony/polyfill-iconv package 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-iconv in production only. But for now, this is a no-brainer for reliability."*

For Engineering Leaders (Technical Feasibility)

*"This polyfill is a drop-in fix for iconv compatibility—no code changes required. Here’s how it fits:

  • Zero-config integration: Add symfony/polyfill-iconv to composer.json, and it auto-replaces missing iconv functions. Existing calls like iconv('UTF-8', 'ASCII//IGNORE', $text) work identically.
  • Targeted use cases:
    • Shared hosting/CI: Resolves flaky tests or deployments where ext-iconv is 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 iconv for large-scale operations (e.g., batch processing). Profile with Blackfire to identify bottlenecks.
    • Not a drop-in for ob_iconv_handler (use mbstring instead).
  • Validation approach:
    1. Test with php -d extension=-iconv artisan test to catch issues early.
    2. Monitor production for iconv() warnings or encoding artifacts (e.g., mojibake).
    3. Consider a feature flag to toggle polyfill usage during rollout.

Alternatives considered:

  • Custom polyfill: Higher maintenance burden and no community support.
  • mbstring only: Requires refactoring all iconv usage (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-mbstring for comprehensive multibyte support."*

For Developers (Implementation)

*"Need iconv to work everywhere? Just run:

composer require symfony/polyfill-iconv

That’s it. The polyfill auto-activates when ext-iconv is missing, so your existing iconv() 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-1 data to UTF-8:
    $title = iconv('ISO-8859-1', 'UTF-8', $record->title);
    

Pro tips:

  • Test locally: Disable ext-iconv to 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
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope