A PHP package to measure the width of unicode strings rendered to a terminal.
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.
Building CLI applications can be challenging when it comes to handling modern Unicode text:
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.
composer require soloterm/grapheme
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)
This library aims to match the behavior of wcwidth()
in modern terminal emulators.
symfony/polyfill-intl-normalizer
package is included as a dependencyext-intl
extension is recommended for best performanceThe library uses a series of optimized patterns and checks to accurately determine character width:
composer test
The test suite includes over 150 different test cases covering many possible Unicode scenarios. Please feel free to add
more.
Contributions are welcome! Please feel free to submit a pull request.
The MIT License (MIT).
This is free! If you want to support me:
Solo was developed by Aaron Francis. If you like it, please let me know!