The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

Grapheme

Grapheme stats

Downloads
159
Stars
38
Open Issues
0
Forks
0

View on GitHub →

A PHP package to measure the width of unicode strings rendered to a terminal.

Grapheme

A highly optimized PHP library for calculating the display width of Unicode graphemes in terminal environments. Accurately determine how many columns a character will occupy in the terminal, including complex emoji, combining marks, and more.

This library was built to support Solo, your all-in-one Laravel command to tame local development.

Why Use This Library?

Building CLI applications can be challenging when it comes to handling modern Unicode text:

  • Emoji and CJK characters take up 2 cells in most terminals
  • Zero-width characters (joiners, marks, etc.) don't affect layout but can cause width calculation errors
  • Complex text like emoji with skin tones or flags require special handling
  • PHP's built-in functions don't fully address these edge cases

This library solves these problems by providing an accurate, performant, and thoroughly tested way to determine the display width of any character or grapheme cluster.

Installation

composer require soloterm/grapheme

Usage

use SoloTerm\Grapheme\Grapheme;
 
// Basic characters (width: 1)
Grapheme::wcwidth('a'); // Returns: 1
Grapheme::wcwidth('ะฏ'); // Returns: 1
 
// East Asian characters (width: 2)
Grapheme::wcwidth('ๆ–‡'); // Returns: 2
Grapheme::wcwidth('ใ‚'); // Returns: 2
 
// Emoji (width: 2)
Grapheme::wcwidth('๐Ÿ˜€'); // Returns: 2
Grapheme::wcwidth('๐Ÿš€'); // Returns: 2
 
// Complex emoji with modifiers (width: 2)
Grapheme::wcwidth('๐Ÿ‘๐Ÿป'); // Returns: 2
Grapheme::wcwidth('๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ'); // Returns: 2
 
// Zero-width characters (width: 0)
Grapheme::wcwidth("\u{200B}"); // Returns: 0 (Zero-width space)
 
// Characters with combining marks (width: 1)
Grapheme::wcwidth('รฉ'); // Returns: 1
Grapheme::wcwidth("e\u{0301}"); // Returns: 1 (e + combining acute)
 
// Special cases
Grapheme::wcwidth("โš \u{FE0E}"); // Returns: 1 (Warning sign in text presentation)
Grapheme::wcwidth("โš \u{FE0F}"); // Returns: 2 (Warning sign in emoji presentation)

Features

  • Highly optimized for performance with early-return paths and smart caching
  • Comprehensive Unicode support including:
    • CJK (Chinese, Japanese, Korean) characters
    • Emoji (including skin tone modifiers, gender modifiers, flags)
    • Zero-width characters and control codes
    • Combining marks and accents
    • Regional indicators and flags
    • Variation selectors
  • Carefully tested against a wide range of Unicode characters
  • Minimal dependencies - only requires PHP 8.2+ and an optional intl extension
  • Compatible with most terminal environments

Terminal Compatibility

This library aims to match the behavior of wcwidth() in modern terminal emulators.

Requirements

  • PHP 8.2 or higher
  • The symfony/polyfill-intl-normalizer package is included as a dependency
  • The ext-intl extension is recommended for best performance

Under the Hood

The library uses a series of optimized patterns and checks to accurately determine character width:

  1. Fast paths for ASCII and zero-width characters
  2. Special handling for complex scripts like Devanagari
  3. Smart detection of emoji and variation selectors
  4. Proper handling of zero-width joiners (ZWJ) and other invisible characters
  5. Caching of results for improved performance

Testing

composer test

The test suite includes over 150 different test cases covering many possible Unicode scenarios. Please feel free to add more.

Contributing

Contributions are welcome! Please feel free to submit a pull request.

License

The MIT License (MIT).

Support

This is free! If you want to support me:

Credits

Solo was developed by Aaron Francis. If you like it, please let me know!

soloterm photo

Solo for Laravel

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Soloterm Grapheme Related Articles

Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup