cybercog/php-unicode 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cybercog/php-unicode

Composer 安装命令:

composer require cybercog/php-unicode

包简介

PHP Unicode library

README 文档

README

Releases Build License

Introduction

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. Requires ext-mbstring.
  • Grapheme level (Grapheme, GraphemeString) — works with user-perceived characters (grapheme clusters). Requires ext-intl.

Requirements

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.

Installation

Pull in the package through Composer.

composer require cybercog/php-unicode

For grapheme cluster support, install the intl PHP extension.

Usage

Code Point

$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('ÿ');

Represent Code Point in any format

$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) "ÿ"

Unicode String (code point level)

$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>

Grapheme (grapheme cluster level)

Requires ext-intl.

$grapheme = \Cog\Unicode\Grapheme::of('👨‍👩‍👧‍👦');

echo strval($grapheme); // (string) "👨‍👩‍👧‍👦"

$codePointList = $grapheme->codePointList; // list<CodePoint>

Grapheme String (grapheme cluster level)

Requires ext-intl.

$string = \Cog\Unicode\GraphemeString::of('Ае👨‍👩‍👧‍👦');

$graphemeList = $string->graphemeList; // list<Grapheme>
// 'А', 'е', '👨‍👩‍👧‍👦' — 3 graphemes (not 9 code points)

echo strval($string); // (string) "Ае👨‍👩‍👧‍👦"

Real-world examples

Convert a character to all supported formats

$codePoint = \Cog\Unicode\CodePoint::of('©');

echo $codePoint->toDecimal();     // 169
echo $codePoint->toHexadecimal(); // "U+00A9"
echo $codePoint->toHtmlEntity();  // "&copy;"
echo $codePoint->toXmlEntity();   // "&#xA9;"

Round-trip between entity formats

$cp = \Cog\Unicode\CodePoint::ofHtmlEntity('&hearts;');

echo $cp->toXmlEntity(); // "&#x2665;"
echo $cp->toDecimal();   // 9829

$cp2 = \Cog\Unicode\CodePoint::ofDecimal($cp->toDecimal());
echo strval($cp2); // "♥"

Inspect code points in a string

$string = \Cog\Unicode\UnicodeString::of('café');

foreach ($string->codePointList as $cp) {
    echo $cp->toHexadecimal() . ' ';
}
// U+0063 U+0061 U+0066 U+00E9

Code points vs. graphemes — why it matters

// 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

Detect combining marks

$acute = \Cog\Unicode\CodePoint::of("\u{0301}"); // combining acute accent
echo $acute->isCombining(); // true

$a = \Cog\Unicode\CodePoint::of('A');
echo $a->isCombining(); // false

Why this library?

PHP 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 / UnicodeString work with individual Unicode code points. Grapheme / GraphemeString work with user-perceived characters (grapheme clusters). Choose the right level for your use case instead of mixing mb_strlen and grapheme_strlen calls.
  • Format conversion. CodePoint converts between character, decimal, hexadecimal (U+XXXX), HTML entity, and XML entity formats in a single object. No need to chain mb_ord, dechex, htmlentities manually.
// 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();

License

About CyberCog

CyberCog is a Social Unity of enthusiasts. Research the best solutions in product & software development is our passion.

CyberCog

cybercog/php-unicode 适用场景与选型建议

cybercog/php-unicode 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.47k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 10 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「emoji」 「unicode」 「symbol」 「grapheme」 「cog」 「code-point」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 cybercog/php-unicode 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cybercog/php-unicode 我们能提供哪些服务?
定制开发 / 二次开发

基于 cybercog/php-unicode 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 12.47k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 12
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-14