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 Intl Grapheme Laravel Package

symfony/polyfill-intl-grapheme

Native PHP polyfill for the Intl Grapheme functions, for working with UTF-8 grapheme clusters. Provides grapheme_strlen, grapheme_substr, grapheme_strpos/stripos, grapheme_extract, grapheme_str_split, and related helpers when ext-intl isn’t available.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Continued strong alignment with Laravel’s multilingual and Unicode needs, particularly for RTL languages and complex grapheme clusters (e.g., emojis, combining characters). The package remains a critical extension for grapheme-aware operations beyond Laravel’s core Str helper.
  • Enhances Laravel’s Blade templating and validation ecosystems by providing robust grapheme-aware utilities, reducing reliance on custom implementations or the intl extension.
  • Future-proofs Laravel apps targeting PHP 8.6+ by leveraging native grapheme_strrev() and other grapheme functions (e.g., grapheme_str_split()), which are now more stable with fixes like #616 for PCRE compatibility.
  • Risk: Overkill for ASCII-only applications or performance-critical paths where native strrev() suffices. The PCRE fix in grapheme_str_split() may introduce minor edge cases in regex-heavy applications.

Integration Feasibility

  • Seamless integration with Laravel’s Str facade via macros, enabling fluent syntax (e.g., Str::graphemeReverse()). The fix for grapheme_str_split() ensures compatibility with regex-based operations (e.g., Laravel’s validation or search features).
  • Minimal code changes required for adoption, with backward compatibility for existing strrev() usage. The new release does not introduce breaking changes, maintaining stability.
  • Blade directives can still be added for RTL content mirroring (e.g., @reverseGraphemes), and validation rules can leverage grapheme-aware reversal for palindrome checks.
  • Risk: PHP 8.6+ requirement remains a barrier for older Laravel versions (pre-11). The PCRE fix may require testing in environments using older PCRE versions (e.g., shared hosting).

Technical Risk

Risk Likelihood Impact Mitigation
PHP 8.6+ dependency High High Document requirement clearly; provide fallback logic (e.g., intl extension or custom polyfill) for older PHP versions.
Performance overhead Low Medium Benchmark against native strrev(); optimize only if critical paths are affected.
Incomplete Unicode support Medium Medium Prefer intl extension if available for broader Unicode coverage (e.g., IntlGraphemeClusterIterator).
Breaking changes in future PHP Low High Monitor PHP deprecations; update polyfill as needed.
Hosting environment constraints Medium High Test in target environments (e.g., shared hosting may lack PHP 8.6 or PCRE updates).
PCRE compatibility issues Low Medium Test grapheme_str_split() with regex patterns in Laravel validation/search.

Key Questions

  1. PHP Version Strategy:

    • Should Laravel enforce PHP 8.6+ for new projects to leverage native grapheme functions, given the PCRE fix in this release?
    • How will we handle legacy systems on PHP < 8.6 (e.g., shared hosting with outdated PCRE)?
  2. Fallback Mechanisms:

    • Should we implement a runtime check for grapheme_str_split() and fall back to preg_split() or intl for environments with PCRE issues?
    • Example:
      function grapheme_str_split_fallback($str, $pattern) {
          return function_exists('grapheme_str_split')
              ? grapheme_str_split($str, $pattern)
              : preg_split($pattern, $str); // or intl-based fallback
      }
      
  3. Laravel Core Integration:

    • Should grapheme_str_split() be added to Laravel’s core Str helper or left as a community package?
    • If core, how will we handle PHP/PCRE version constraints?
  4. Testing Scope:

    • What RTL languages/emojis should be tested with grapheme_str_split() (e.g., Arabic, Hebrew, emoji sequences like 👨‍👩‍👧‍👦)?
    • Should we add regression tests for grapheme_str_split() with complex regex patterns (e.g., \p{L}+)?
  5. Documentation:

    • How will we highlight the PHP 8.6+ and PCRE compatibility requirements in Laravel docs?
    • Should we provide a migration guide for upgrading from intl-based solutions or custom regex splits?

Integration Approach

Stack Fit

  • Laravel Ecosystem:
    • Str Facade: Extend with graphemeSplit() macro for fluent regex splitting:
      Str::macro('graphemeSplit', function ($pattern) {
          return grapheme_str_split($this->value, $pattern);
      });
      
    • Blade: Add @graphemeSplit directive for RTL content processing.
    • Validation: Create grapheme_split rule for complex pattern validation (e.g., usernames with RTL characters).
    • Search: Integrate with Laravel Scout/Elasticsearch for grapheme-aware token splitting.
  • PHP 8.6+: Native grapheme_str_split() is used; PCRE fix ensures compatibility with regex patterns.
  • PHP < 8.6: Fallback to intl extension or custom polyfill (not recommended for production).

Migration Path

  1. Assessment Phase (1 day):

    • Audit existing preg_split()/explode() usage in codebase, especially for RTL/emoji-heavy features.
    • Identify regex patterns used in validation/search (e.g., \p{L}+, [^\s]+).
    • Check PHP/PCRE version compatibility across environments.
  2. Integration Phase (3–5 days):

    • Step 1: Add symfony/polyfill-intl-grapheme to composer.json (PHP 8.6+ only).
    • Step 2: Extend Str helper with graphemeSplit():
      Str::macro('graphemeSplit', function ($pattern) {
          return grapheme_str_split($this->value, $pattern);
      });
      
    • Step 3: Add Blade directive:
      Blade::directive('graphemeSplit', function ($expression, $pattern) {
          return "<?php echo implode(',', grapheme_str_split({$expression}, {$pattern})); ?>";
      });
      
    • Step 4: Implement validation rule (optional):
      Rule::macro('graphemeSplit', function ($pattern) {
          return function ($attribute, $value, $fail) {
              $parts = grapheme_str_split($value, $pattern);
              // Custom logic (e.g., check part lengths)
          };
      });
      
  3. Testing Phase (2–3 days):

    • Test RTL languages (Arabic, Hebrew, Persian) with regex patterns.
    • Test emoji sequences (e.g., 👨‍👩‍👧‍👦 split by whitespace).
    • Test combining characters (e.g., éléphant split by vowels).
    • Verify backward compatibility for ASCII strings and simple regex.
    • Test PCRE edge cases (e.g., nested quantifiers, lookarounds).
  4. Deployment Phase (1 day):

    • Update composer.json and run composer update.
    • Deploy to staging; test in RTL-heavy workflows and regex-based validations.
    • Monitor performance (grapheme splitting should add <5ms per operation for typical patterns).

Compatibility

  • PHP 8.6+: Full support; PCRE fix resolves grapheme_str_split() issues.
  • PHP 8.2–8.5: Partial support via symfony/polyfill-intl-grapheme (but grapheme_str_split() may still fail on older PCRE).
  • PHP < 8.2: Unsupported; use intl extension or custom logic.
  • Laravel Versions:
    • Laravel 11+: Native support (PHP 8.2+).
    • Laravel 10/9: Requires PHP 8.6 or intl extension + PCRE updates.
  • Dependencies: No conflicts with Laravel core or popular packages.

Sequencing

Prioritization for Laravel Projects:

  1. RTL Language Support:
    • Validation rules for RTL usernames/passwords using grapheme_str_split().
    • Search/autocomplete for RTL content (e.g., split tokens for Elasticsearch).
  2. Emoji/Combining Characters:
    • Chat applications (split messages by grapheme clusters).
    • Social media integrations (e
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.
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
anil/file-picker
broqit/fields-ai