The problem is converting the string to untrusted code (in PHP, using phlyLabs punycode string converter)

I use the code from here: http://phlymail.com/en/downloads/idna/download/ and built such a function (from an 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 does not work properly.

For the input: 
It gives: Ð ΓΒΎΓ‘Γ‘ΓΒΈΓ‘
Whereas it should give: xn--h1alffa3f

What am I doing wrong? The $ inputstring that is passed is a regular string without special declarations / etc ...

0
source share
4 answers

UTF-8? . ISO-8859-5? PHP utf8_encode(), , ISO-88591-1 (ISO Latin-1, ). transcode_wrapper.php, . .

+3
0

I would just add something like using a module if possible, otherwise the function proposed by Dave is:

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);}
}
0
source

Try this method to convert encoding

//$inputstringutf8 = utf8_encode($inputstring);

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

Source: https://habr.com/ru/post/1754064/


All Articles