0

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...

Dave
  • 599
  • 4
  • 11
  • 20

4 Answers4

3

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.

  • Hi! Thank you! That did seem to be the case and it solved the problem! Muchos gracias! – Dave Jun 28 '10 at 16:41
0

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);}
}
llange
  • 757
  • 2
  • 10
  • 14
0

you might need PHP IDNA Extension

Sergey Eremin
  • 10,994
  • 2
  • 38
  • 44
0

Try this method to convert encoding

//$inputstringutf8 = utf8_encode($inputstring);

$inputstringutf8 = mb_convert_encoding($inputstring, 'utf-8', mb_detect_encoding($inputstring));
ADyson
  • 57,178
  • 14
  • 51
  • 63