Php 简明教程
PHP – IntlChar
在 PHP7 中,引入了新的 IntlChar 类。该类提供对可用于访问 Unicode 字符相关信息的大量实用工具方法的访问权限。Intl 类中有很多静态方法和常量。它们紧密遵守底层 ICU(Unicode 的国际组件)库所使用的名称和行为。
In PHP7, a new IntlChar class has been introduced. It provides access to a number of utility methods that can be used to access information about Unicode characters. There are a number of static methods and constants in Intl class. They adhere closely to the names and behavior used by the underlying ICU (International Components for Unicode) library.
Note 您需要在系统中的 PHP 程序中启用 Intl 扩展。要启用它,请打开 php.ini 文件并取消注释(从该行删除前导分号)。
Note that you need to enable the Intl extension in the PHP installation in your system. To enable, open php.ini file and uncomment (remove the leading semicolon from the line)
extension=intl
以下内容通过示例展示 Intl 类中的一些静态函数:
Some static functions from Intl class are explained with examples as below −
IntlChar::charAge
此函数获取码点的“年代”
This function gets the "age" of the code point
public static IntlChar::charAge(int|string $codepoint): ?array
“年代”表示将码点首次指定(作为非字符或私用)或分配字符后的 Unicode 版本。
The "age" is the Unicode version when the code point was first designated (as a non-character or for Private Use) or assigned a character.
IntlChar::charFromName
charFromName() 函数通过名称查找 Unicode 字符并返回其码点值。
The charFromName() function finds Unicode character by name and return its code point value
public static IntlChar::charFromName(string $name,
int $type = IntlChar::UNICODE_CHAR_NAME): ?int
使用查找时要用的名称类型参数组。可以是以下任何一个常量 -
The type parameter sets of names to use for the lookup. Can be any of these constants −
-
IntlChar::UNICODE_CHAR_NAME (default)
-
IntlChar::UNICODE_10_CHAR_NAME
-
IntlChar::EXTENDED_CHAR_NAME
-
IntlChar::CHAR_NAME_ALIAS
-
IntlChar::CHAR_NAME_CHOICE_COUNT
IntlChar::charName
charName() 函数检索 Unicode 字符的名称
The charName() function retrieves the name of a Unicode character
public static IntlChar::charName(int|string $codepoint,
int $type = IntlChar::UNICODE_CHAR_NAME): ?string
IntlChar::isalpha
isalpha() 函数确定指定代码点是否是字母字符。对于通用类别“L”(字母)返回 true。
The isalpha() function determines whether the specified code point is a letter character. true for general categories "L" (letters).
public static IntlChar::isalpha(int|string $codepoint): ?bool
Example
请看以下示例:
Take a look at the following example −
<?php
var_dump(IntlChar::isalpha("A"));
var_dump(IntlChar::isalpha("1"));
?>
它将生成以下 output −
It will produce the following output −
bool(true)
bool(false)
Intl 类定义了类似的静态方法,例如 isdigit()、isalnum()、isblank() 等等。
The Intl class defines similar static methods such as isdigit(), isalnum(), isblank(), etc.
IntlChar::islower
islower() 函数确定指定代码点是否具有通用类别“Ll”(小写字母)。
The islower() function determines whether the specified code point has the general category "Ll" (lowercase letter).
public static IntlChar::islower(int|string $codepoint): ?bool
Example
请看以下示例:
Take a look at the following example −
<?php
var_dump(IntlChar::islower("A"));
var_dump(IntlChar::islower("a"));
?>
它将生成以下 output −
It will produce the following output −
bool(false)
bool(true)
同样,还有如下函数:isupper()、istitle()、iswhitespace() 等等。
Similarly, there are functions such as isupper(), istitle(), iswhitespace() etc.