mlocati / unipoints
PHP的Unicode码位库
1.2.0
2024-09-16 09:14 UTC
Requires
- php: ^8.1
Requires (Dev)
- phpunit/phpunit: ^9.6
This package is auto-updated.
Last update: 2024-09-16 09:20:49 UTC
README
PHP的Unicode码位库
简化的Unicode术语
码位
码位是字符、空格、符号、标点、分隔符等,即组成文本的单个单元。
区块
码位被分组到区块中,即连续的码位组,它们是公共集合的一部分。
示例
- a 包含在
基本拉丁
区块中 - α 包含在
希腊和科普特
区块中 - 𝅘𝅥𝅮 包含在
音乐符号
区块中 - ↩ 包含在
箭头
区块中 - ☂ 包含在
杂项符号
区块中
平面
平面是由65,536个连续的码位组成的区块,可能包含零个、一个或多个区块。
一般类别
此库还提供了每个码位的一般类别,即您可以知道一个码位是否为小写字母、符号、标点等。
代理码位
为了扩展16位可以表示的码位数量,Unicode引入了“代理”。单个字符(或标点、...)可以通过组合两个连续的代理(称为“高代理”和“低代理”)来表示。这意味着这样的码位仅在成对的情况下才有意义。
示例用法
码位在基于字符串的MLUnipoints\Codepoint
枚举中列出。枚举案例字符串的值包含Unicode符号:这样,例如,为了获取a
的情况,您可以简单地写
use MLUnipoints\Codepoint; $codepoint = Codepoint::from('a');
由于MLUnipoints\Codepoint
枚举相当大(当您自动加载它时,它可以使用数十MB的内存),您还可以使用在MLUnipoints\Codepoint
命名空间下定义的特定于区块的实例(但这需要您事先知道区块)。例如
use MLUnipoints\Codepoint; $codepoint = Codepoint\Basic_Latin::from('a');
MLUnipoints\Codepoint
枚举的每个案例都有一个MLUnipoints\Info\CodepointInfo
属性。您可以通过编写轻松检索此属性
use MLUnipoints\Codepoint; use MLUnipoints\Info\CodepointInfo; $codepoint = Codepoint::from('a'); $codepointInfo = CodepointInfo::from(Codepoint::from('a'));
此属性提供了码位的数值、Unicode名称、一般类别,以及(如果您不使用特定于区块的枚举)区块。
您也可以以类似的方式获取区块、平面和一般类别的详细信息。
例如,以下代码
use MLUnipoints\Codepoint; use MLUnipoints\Info\BlockInfo; use MLUnipoints\Info\CategoryInfo; use MLUnipoints\Info\CodepointInfo; use MLUnipoints\Info\PlaneInfo; $codepoint = Codepoint::from('a'); $codepointInfo = CodepointInfo::from($codepoint); $categoryInfo = CategoryInfo::from($codepointInfo->category); $blockInfo = BlockInfo::from($codepointInfo->block); $planeInfo = PlaneInfo::from($blockInfo->plane); echo 'Codepoint: ', $codepointInfo->id, "\n"; echo 'Codepoint name: ', $codepointInfo->name, "\n"; echo 'Codepoint general category: ', $categoryInfo->description, "\n"; foreach ($categoryInfo->parentCategories as $parentCategory) { echo 'Codepoint parent general category: ', CategoryInfo::from($parentCategory)->description, "\n"; } echo 'Block name: ', $blockInfo->name, "\n"; echo 'Plane name: ', $planeInfo->name, "\n"; echo 'Plane short name: ', $planeInfo->shortName, "\n";
将输出
Codepoint: 97
Codepoint name: LATIN SMALL LETTER A
Codepoint general category: a lowercase letter
Codepoint parent general category: a cased letter
Codepoint parent general category: a letter
Block name: Basic Latin
Plane name: Basic Multilingual Plane
Plane short name: BMP
您还可以使用Unicode枚举来打印字符和符号。
例如
use MLUnipoints\Codepoint; echo Codepoint::SUN_BEHIND_CLOUD->value;
将打印
⛅