rowbot/punycode

适用于国际域名名称在应用程序(IDNA)中的Unicode Bootstring编码。

1.0.4 2024-05-03 00:56 UTC

This package is auto-updated.

Last update: 2024-09-03 01:37:38 UTC


README

Software License GitHub Workflow Status Code Coverage PHP Version Total Downloads

PHP中RFC 3492 Punycode的实现,基于附录C中的示例实现。这不是 idn_to_asciiidn_to_utf8 的替代品。

要求

  • PHP 7.1+

安装

composer require rowbot/punycode

API

Punycode::decode(string $input, int $outLength = null, array &$caseFlags = [])

Punycode::decode() 方法接受一个ASCII编码的字符串,并将其解码为UTF-8编码的字符串。可选地,第二个参数可以指定以限制返回字符串的大小。

参数

  • $input - 要转换为UTF-8编码字符串的ASCII编码的punycode字符串。
  • $outLength - 一个表示结果输出字符串最大长度的正整数,以代码点为单位。默认为2,147,483,647。
  • $caseFlags - 一个数组,将每个字符的case标志插入到其中。

抛出异常

  • \Rowbot\Punycode\Exception\OutputSizeExceededException - 如果输出字符串的大小超过指定的最大大小。
  • \Rowbot\Punycode\Exception\OverflowException - 如果发生整数溢出。
  • \Rowbot\Punycode\Exception\InvalidInputException - 如果输入包含非ASCII字节或映射代码点到数字失败。
use Rowbot\Punycode\Punycode;

try {
    echo Punycode::decode('Hello-Another-Way--fc4qua05auwb3674vfr0b'); // Hello-Another-Way-それぞれの場所
} catch (\Rowbot\Punycode\Exception\PunycodeException $e) {
    echo 'An error occured!';
}

Punycode::encode(string $input, int $outLength = null, array $caseFlags = [])

Punycode::encode() 方法接受一个UTF-8编码的字符串,并将其转换为ASCII编码的punycode字符串。可选地,第二个参数可以指定以限制返回字符串的大小。

参数

  • $input - 要转换为punycode的UTF-8编码的字符串。
  • $outLength - 一个表示结果输出字符串最大长度的正整数,以代码点为单位。默认为2,147,483,647。
  • $caseFlags - 一个布尔值数组,其中true表示字符应该是大写,false表示字符应该是小写。这仅影响ASCII字符 [a-zA-Z]

抛出异常

  • \Rowbot\Punycode\Exception\OutputSizeExceededException - 如果输出字符串的大小超过指定的最大大小。
  • \Rowbot\Punycode\Exception\OverflowException - 如果发生整数溢出。
use Rowbot\Punycode\Punycode;

try {
    echo Punycode::encode('Hello-Another-Way-それぞれの場所'); // Hello-Another-Way--fc4qua05auwb3674vfr0b
} catch (\Rowbot\Punycode\Exception\PunycodeException $e) {
    echo 'An error occured!';
}