phpwhois / idna-convert
This package is not auto-updated.
Last update: 2022-02-01 12:43:05 UTC
README
http://idnaconv.phlymail.de phlymail@phlylabs.de
(c) 2004-2014 phlyLabs, 柏林
简介
类 idna_convert 允许将国际化域名(请参阅 RFC 3490、3491、3492 和 3454 以获取详细信息)转换为它们可以在全球各个注册处使用的形式,以便在原始(本地化)形式和 DNS(域名系统)中使用的编码形式之间进行转换。
该类提供了两个公共方法,encode() 和 decode(),它们确实执行您期望它们执行的操作。您可以使用完整的域名、简单的字符串和完整的电子邮件地址。这意味着,您可以使用以下任何一种表示法:
- www.nörgler.com
- xn--nrgler-wxa
- xn--brse-5qa.xn--knrz-1ra.info
错误、编码不正确或无效的字符串将导致在严格模式下返回 FALSE 响应或仅部分转换的字符串。您可以通过调用 get_last_error() 方法来查询发生的错误。
Unicode 字符串应期望为 UTF-8 字符串、UCS-4 字符串或 UCS-4 数组。默认格式是 UTF-8。要设置不同的编码,您可以使用 setParams() 方法 - 请参阅内联文档以获取详细信息。ACE 字符串(Punycode 形式)始终是 7 位 ASCII 字符串。
注意:从版本 0.6.0 开始,此类使用 PHP5 的 OOP 风格编写。由于 PHP4 已不再积极维护,您应尽快切换到 PHP5。我们预计它也将与即将推出的 PHP6 兼容。
注意:向后不兼容!从版本 0.6.4 开始,该类默认允许德语连字符 ß 以 DeNIC(.DE 的注册机构)的形式进行编码。在旧版本中,“ß”被映射为“ss”。如果您仍然需要此行为,请参阅下面的示例 5。
注意:从版本 0.8.0 开始,该类完全支持 IDNA 2008。因此,上述参数已弃用,并由一个参数替换,以在标准之间切换。请参阅下面的更新示例 5。
文件
idna_convert.class.php - 实际类
example.php - 用于转换的示例网页
transcode_wrapper.php - 转换各种编码,请参见下面
uctc.php - phlyLabs 的 Unicode 转码器,请参见下面
ReadMe.txt - 此文件
LICENCE - LGPL 许可文件
类包含在 idna_convert.class.php 中。
示例
- 假设我们希望编码域名 nörgler.com
// Include the class require_once('idna_convert.class.php'); // Instantiate it $IDN = new idna_convert(); // The input string, if input is not UTF-8 or UCS-4, it must be converted before $input = utf8_encode('nörgler.com'); // Encode it to its punycode presentation $output = $IDN->encode($input); // Output, what we got now echo $output; // This will read: xn--nrgler-wxa.com
- 我们从一个 punycoded 域名收到了一封电子邮件,并愿意了解域名最初是如何读的
// Include the class require_once('idna_convert.class.php'); // Instantiate it $IDN = new idna_convert(); // The input string $input = 'andre@xn--brse-5qa.xn--knrz-1ra.info'; // Encode it to its punycode presentation $output = $IDN->decode($input); // Output, what we got now, if output should be in a format different to UTF-8 // or UCS-4, you will have to convert it before outputting it echo utf8_decode($output); // This will read: andre@börse.knörz.info
- 输入是从一个 UCS-4 编码的文件中读取的,并逐行编码。通过附加可选的第二个参数,我们告诉 enode() 要使用的输入格式
// Include the class require_once('idna_convert.class.php'); // Instantiate it $IDN = new dinca_convert(); // Iterate through the input file line by line foreach (file('ucs4-domains.txt') as $line) { echo $IDN->encode(trim($line), 'ucs4_string'); echo "\n"; }
- 我们希望将整个 URI 转换为 IDNA 形式,但保留其路径或查询字符串组件。仅使用 encode() 会导致路径或查询字符串损坏。这里公开方法 encode_uri() 就派上用场了
// Include the class require_once('idna_convert.class.php'); // Instantiate it $IDN = new idna_convert(); // The input string, a whole URI in UTF-8 (!) $input = 'http://nörgler:secret@nörgler.com/my_päth_is_not_ÄSCII/'); // Encode it to its punycode presentation $output = $IDN->encode_uri($input); // Output, what we got now echo $output; // http://nörgler:secret@xn--nrgler-wxa.com/my_päth_is_not_ÄSCII/
- 为了支持IDNA 2008,类需要使用额外的参数进行调用。这也可以在实例上实现。
// Include the class require_once('idna_convert.class.php'); // Instantiate it $IDN = new idna_convert(array('idn_version' => 2008)); // Sth. containing the German letter ß $input = 'meine-straße.de'); // Encode it to its punycode presentation $output = $IDN->encode_uri($input); // Output, what we got now echo $output; // xn--meine-strae-46a.de // Switch back to old IDNA 2003, the original standard $IDN->set_parameter('idn_version', 2003); // Sth. containing the German letter ß $input = 'meine-straße.de'); // Encode it to its punycode presentation $output = $IDN->encode_uri($input); // Output, what we got now echo $output; // meine-strasse.de
转码包装器
如果您有ISO-8859-1和UTF-8之外的编码字符串,您可能需要在这些字符串转换为UTF-8之后再传递给IDNA转换器。PHP内置的utf8_encode()和utf8_decode()函数只能处理ISO-8859-1。请使用transcode_wrapper.php文件进行转换。它需要安装iconv、libiconv或mbstring,并安装相关的一个PHP扩展。您可能会发现encode_utf8()函数可以替代utf8_encode(),decode_utf8()函数可以替代utf8_decode()。
示例用法
<?php require_once('idna_convert.class.php'); require_once('transcode_wrapper.php'); $mystring = '<something in e.g. ISO-8859-15'; $mystring = encode_utf8($mystring, 'ISO-8859-15'); echo $IDN->encode($mystring); ?>
UCTC - Unicode 转码器
当处理一种或多种Unicode编码格式时,您可能会发现这个类很有用。该类是静态的,需要PHP5。它可以相互转换
- UCS-4字符串/数组
- UTF-8
- UTF-7
- UTF-7 IMAP(修改后的UTF-7)所有编码在返回时都期望/返回给定格式的字符串,只有一个主要例外:UCS-4数组只是一个数组,其中每个值代表字符串中的一个码点,即每个值是一个32位整数。
示例用法
<?php require_once('uctc.php'); $mystring = 'nörgler.com'; echo uctc::convert($mystring, 'utf8', 'utf7imap'); ?>
联系我们
如有错误、bug、问题或愿望,请通过上述电子邮件地址与我们联系。
phlyLabs 团队 http://phlylabs.de 邮箱:phlymail@phlylabs.de