将字符串转换为 puny 代码时出现问题(在 PHP 中,使用 phlyLabs 的 punycode 字符串转换器)

发布于 2024-09-06 22:35:17 字数 722 浏览 3 评论 0原文

我正在使用这里的代码: http://phlymail.com/en/downloads/ idna/download/ 并构建了一个像这样的函数(来自示例):

function convert_to_punycode($inputstring)
{
    $IDN = new idna_convert();
    // The input string, if input is not UTF-8 or UCS-4, it must be converted before
    $inputstringutf8 = utf8_encode($inputstring);
    // Encode it to its punycode presentation
    $outputstringpunycode = $IDN->encode($inputstringutf8);
    return $outputstringpunycode;
}

但是它无法正常工作。

输入:Россию
它给出:
而它应该给出: xn--h1alffa3f

我做错了什么?正在传递的 $inputstring 是一个普通字符串,没有特殊声明/等等...

i'm using the code from here: http://phlymail.com/en/downloads/idna/download/ and built a function like this (from the example):

function convert_to_punycode($inputstring)
{
    $IDN = new idna_convert();
    // The input string, if input is not UTF-8 or UCS-4, it must be converted before
    $inputstringutf8 = utf8_encode($inputstring);
    // Encode it to its punycode presentation
    $outputstringpunycode = $IDN->encode($inputstringutf8);
    return $outputstringpunycode;
}

However it doesnt work properly.

For the input: Россию
It gives: РоÑÑиÑ
Whereas it should give: xn--h1alffa3f

What am I doing wrong? $inputstring which is being passed is a normal string with no special declarations/etc...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼眸印温柔 2024-09-13 22:35:17

你的字符串已经是UTF-8了吗?看起来是这样。
还是 ISO-8859-5 中的内容?
在这两种情况下,您都不能使用 PHP 函数 utf8_encode(),因为它要求您的输入字符串为 ISO-88591-1(ISO Latin-1,西欧语言)。查看文件 transcode_wrapper.php,该文件随类源一起提供。这可能对你有帮助。

Is your string already UTF-8? Looks like it.
Or is it in ISO-8859-5?
In both cases you cannot use the PHP function utf8_encode(), since it expects your input string to be ISO-88591-1 (ISO Latin-1, Western European languages). Look into the file transcode_wrapper.php, which is delivered with the class source. This might help you.

悲喜皆因你 2024-09-13 22:35:17

您可能需要 PHP IDNA 扩展

you might need PHP IDNA Extension

感情废物 2024-09-13 22:35:17

我只是添加一些东西,比如如果可能的话使用模块,否则戴夫建议的功能:

if(!function_exists('idn_to_ascii') and !function_exists('idn_to_utf8'))
{   define('IDN_FALLBACK_VERSION',2008);
    require_once('idna_convert.class.php');
    function idn_to_ascii($string)
    {   $IDN = new idna_convert(array('idn_version'=>IDN_FALLBACK_VERSION));
        return $IDN->encode($string);
    }
    function idn_to_utf8($string)
    {   $IDN = new idna_convert(array('idn_version'=>IDN_FALLBACK_VERSION));
        return $IDN->decode($string);
    }
    function idn_to_unicode($string){return idn_to_utf8($string);}
}

I'd just add something like to use if possible the module otherwise Dave suggested function :

if(!function_exists('idn_to_ascii') and !function_exists('idn_to_utf8'))
{   define('IDN_FALLBACK_VERSION',2008);
    require_once('idna_convert.class.php');
    function idn_to_ascii($string)
    {   $IDN = new idna_convert(array('idn_version'=>IDN_FALLBACK_VERSION));
        return $IDN->encode($string);
    }
    function idn_to_utf8($string)
    {   $IDN = new idna_convert(array('idn_version'=>IDN_FALLBACK_VERSION));
        return $IDN->decode($string);
    }
    function idn_to_unicode($string){return idn_to_utf8($string);}
}
み零 2024-09-13 22:35:17

尝试这个方法来转换编码

//$inputstringutf8 = utf8_encode($inputstring);

$inputstringutf8 = mb_convert_encoding($inputstring, 'utf-8', mb_detect_encoding($inputstring));

Try this method to convert encoding

//$inputstringutf8 = utf8_encode($inputstring);

$inputstringutf8 = mb_convert_encoding($inputstring, 'utf-8', mb_detect_encoding($inputstring));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文