【问题标题】:Problem in converting string to puny code (in PHP, using phlyLabs's punycode string converter)将字符串转换为 puny 代码时出现问题(在 PHP 中,使用 phlyLabs 的 punycode 字符串转换器)
【发布时间】:2010-06-28 12:43:11
【问题描述】:

我正在使用这里的代码: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 是一个没有特殊声明/等的普通字符串...

【问题讨论】:

    标签: php utf-8 punycode


    【解决方案1】:

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

    【讨论】:

    • 嗨!谢谢!似乎确实如此,它解决了问题!非常感谢!
    【解决方案2】:

    你可能需要PHP IDNA Extension

    【讨论】:

      【解决方案3】:

      如果可能的话,我会添加类似使用模块的东西,否则 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);}
      }
      

      【讨论】:

        【解决方案4】:

        试试这个方法转换编码

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-03-17
          • 1970-01-01
          • 2021-12-05
          • 2022-11-11
          • 2018-11-13
          • 2022-01-08
          • 1970-01-01
          相关资源
          最近更新 更多