Streamline Unicode strings, code points and grapheme clusters manipulations. Object oriented implementation.
The library provides two levels of abstraction:
- Code point level (
CodePoint,UnicodeString) — works with individual Unicode code points. Requiresext-mbstring. - Grapheme level (
Grapheme,GraphemeString) — works with user-perceived characters (grapheme clusters). Requiresext-intl.
| Class | Required Extensions |
|---|---|
CodePoint |
ext-mbstring |
UnicodeString |
ext-mbstring |
Grapheme |
ext-mbstring, ext-intl |
GraphemeString |
ext-mbstring, ext-intl |
PHP 8.1 or higher is required.
Pull in the package through Composer.
composer require cybercog/php-unicodeFor grapheme cluster support, install the intl PHP extension.
$codePoint = \Cog\Unicode\CodePoint::of('ÿ');
$codePoint = \Cog\Unicode\CodePoint::ofDecimal(255);
$codePoint = \Cog\Unicode\CodePoint::ofHexadecimal('U+00FF');
$codePoint = \Cog\Unicode\CodePoint::ofHtmlEntity('ÿ');
$codePoint = \Cog\Unicode\CodePoint::ofXmlEntity('ÿ');$codePoint = \Cog\Unicode\CodePoint::of('ÿ');
echo strval($codePoint); // (string) "ÿ"
echo $codePoint->toDecimal(); // (int) 255
echo $codePoint->toHexadecimal(); // (string) "U+00FF"
echo $codePoint->toHtmlEntity(); // (string) "ÿ"
echo $codePoint->toXmlEntity(); // (string) "ÿ"$string = \Cog\Unicode\UnicodeString::of('Hello');UnicodeString object will contain a list of code points.
For example, the Unicode string "Hello" is represented by the code points:
- U+0048 (H)
- U+0065 (e)
- U+006C (l)
- U+006C (l)
- U+006F (o)
echo strval($string); // (string) "Hello"
$codePointList = $string->codePointList; // list<CodePoint>Requires ext-intl.
$grapheme = \Cog\Unicode\Grapheme::of('👨👩👧👦');
echo strval($grapheme); // (string) "👨👩👧👦"
$codePointList = $grapheme->codePointList; // list<CodePoint>Requires ext-intl.
$string = \Cog\Unicode\GraphemeString::of('Ае👨👩👧👦');
$graphemeList = $string->graphemeList; // list<Grapheme>
// 'А', 'е', '👨👩👧👦' — 3 graphemes (not 9 code points)
echo strval($string); // (string) "Ае👨👩👧👦"$codePoint = \Cog\Unicode\CodePoint::of('©');
echo $codePoint->toDecimal(); // 169
echo $codePoint->toHexadecimal(); // "U+00A9"
echo $codePoint->toHtmlEntity(); // "©"
echo $codePoint->toXmlEntity(); // "©"$cp = \Cog\Unicode\CodePoint::ofHtmlEntity('♥');
echo $cp->toXmlEntity(); // "♥"
echo $cp->toDecimal(); // 9829
$cp2 = \Cog\Unicode\CodePoint::ofDecimal($cp->toDecimal());
echo strval($cp2); // "♥"$string = \Cog\Unicode\UnicodeString::of('café');
foreach ($string->codePointList as $cp) {
echo $cp->toHexadecimal() . ' ';
}
// U+0063 U+0061 U+0066 U+00E9// Flag emoji: 2 code points, but 1 visible character
$flag = \Cog\Unicode\UnicodeString::of('🇦🇶');
echo count($flag->codePointList); // 2
$flag = \Cog\Unicode\GraphemeString::of('🇦🇶');
echo count($flag->graphemeList); // 1
// Family emoji: 7 code points (persons + ZWJ), 1 visible character
$family = \Cog\Unicode\GraphemeString::of('👨👩👧👦');
echo count($family->graphemeList); // 1
$familyGrapheme = $family->graphemeList[0];
echo count($familyGrapheme->codePointList); // 7$acute = \Cog\Unicode\CodePoint::of("\u{0301}"); // combining acute accent
echo $acute->isCombining(); // true
$a = \Cog\Unicode\CodePoint::of('A');
echo $a->isCombining(); // falsePHP provides mb_* and grapheme_* functions, but they are procedural and return raw strings. This library wraps them in immutable, type-safe value objects with two key benefits:
- Two levels of abstraction.
CodePoint/UnicodeStringwork with individual Unicode code points.Grapheme/GraphemeStringwork with user-perceived characters (grapheme clusters). Choose the right level for your use case instead of mixingmb_strlenandgrapheme_strlencalls. - Format conversion.
CodePointconverts between character, decimal, hexadecimal (U+XXXX), HTML entity, and XML entity formats in a single object. No need to chainmb_ord,dechex,htmlentitiesmanually.
// Procedural
$char = '©';
$dec = mb_ord($char);
$hex = 'U+' . strtoupper(sprintf('%04X', $dec));
$html = htmlentities($char, ENT_HTML5 | ENT_QUOTES);
// With this library
$cp = \Cog\Unicode\CodePoint::of('©');
$dec = $cp->toDecimal();
$hex = $cp->toHexadecimal();
$html = $cp->toHtmlEntity();PHP Unicodepackage is open-sourced software licensed under the MIT license by Anton Komarev.
CyberCog is a Social Unity of enthusiasts. Research the best solutions in product & software development is our passion.
