2010-06-28 3 views
0

J'utilise le code d'ici:problème dans la chaîne de conversion au code chétif (en PHP, en utilisant le convertisseur de chaîne punycode de phlyLabs)

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; 
} 

Cependant: http://phlymail.com/en/downloads/idna/download/ et construit une fonction comme celui-ci (l'exemple) ça ne marche pas correctement.

For the input: Россию 
It gives: РоÑÑÐ¸Ñ 
Whereas it should give: xn--h1alffa3f 

Qu'est-ce que je fais mal? $ inputstring qui est passé est une chaîne normale sans déclaration spéciale/etc ...

Répondre

3

Votre chaîne est-elle déjà UTF-8? On dirait ça. Ou est-ce en ISO-8859-5? Dans les deux cas, vous ne pouvez pas utiliser la fonction PHP utf8_encode(), car elle s'attend à ce que votre chaîne d'entrée soit ISO-88591-1 (ISO Latin-1, langues d'Europe occidentale). Regardez dans le fichier transcode_wrapper.php, qui est livré avec la source de la classe. Cela pourrait vous aider.

+0

Salut ! Je vous remercie! Cela semblait être le cas et cela a résolu le problème! Muchos gracias! – Dave

0

Je voudrais juste ajouter quelque chose comme d'utiliser si possible le module autre fonction suggéré 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);} 
} 
0

Essayez cette méthode pour convertir l'encodage

//$inputstringutf8 = utf8_encode($inputstring); 

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