将字符串转换为微弱代码的问题(在PHP中,使用phlyLabs的punycode字符串转换器)

我正在使用这里的代码: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;
}
但它无法正常工作。
For the input: Россию
It gives: РоÑÑиÑ
Whereas it should give: xn--h1alffa3f
我究竟做错了什么?正在传递的$ inputstring是一个普通的字符串,没有特殊的声明/ etc ...     
已邀请:
你的字符串是否已经是UTF-8了?看起来像。 还是ISO-8859-5? 在这两种情况下,您都不能使用PHP函数utf8_encode(),因为它希望您的输入字符串是ISO-88591-1(ISO Latin-1,西欧语言)。查看文件transcode_wrapper.php,它随类源一起提供。这可能对你有帮助。     
你可能需要PHP IDNA扩展     
我只是添加一些类似于使用的东西,如果可能的话,否则Dave建议的功能:
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);}
}
    
尝试使用此方法转换编码
//$inputstringutf8 = utf8_encode($inputstring);

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

要回复问题请先登录注册