将字符串转换为 puny 代码时出现问题(在 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;
}
但是它无法正常工作。
输入:Россию 它给出: 而它应该给出: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的字符串已经是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.
您可能需要 PHP IDNA 扩展
you might need PHP IDNA Extension
我只是添加一些东西,比如如果可能的话使用模块,否则戴夫建议的功能:
I'd just add something like to use if possible the module otherwise Dave suggested function :
尝试这个方法来转换编码
Try this method to convert encoding