PHP 5.6.24 is released


(PHP 5 >= 5.3.0, PHP 7, PECL intl >= 1.0.2, PHP 7, PECL idn >= 0.1)

idn_to_asciiConvertie un nom de domaine au format IDNA ASCII


Style procédural

string idn_to_ascii ( string $domain [, int $options = 0 [, int $variant = INTL_IDNA_VARIANT_2003 [, array &$idna_info ]]] )

Cette fonction convertit un nom de domaine Unicode au format IDNA ASCII-compatible.

Liste de paramètres


Domaine à convertir. En PHP 5, il doit être encodé UTF-8.


Options de conversion - combinaison de constantes IDNA_*. (sauf les constantes IDNA_ERROR_*).


Soit la constante INTL_IDNA_VARIANT_2003 pour IDNA 2003, soit la constante INTL_IDNA_VARIANT_UTS46 pour UTS #46.


Ce paramètre ne peut être utilisé que si la cosntante INTL_IDNA_VARIANT_UTS46 a été utilisée dans le paramètre variant. Dans ce cas, ce sera un tableau avec la clé 'result' contenant le résultat de la transformation, la clé 'isTransitionalDifferent' contenant un booléen indiquant l'utilisation du mécanisme transitionnel UTS #46 ayant altéré ou non le résultat, et la clé 'errors' contenant un int représentant un jeu de bits des constantes d'erreurs IDNA_ERROR_*.

Valeurs de retour

Le nom de domaine encodé au format ASCII-compatible. ou FALSE si une erreur survient


Version Description
5.4.0/PECL 2.0.0b1 Ajout du paramètre variant et du paramètre idna_info ; Support de l'UTS #46.


Exemple #1 Exemple avec idn_to_ascii()


echo idn_to_ascii('tä'); 


L'exemple ci-dessus va afficher :

Voir aussi

  • idn_to_unicode()

add a note add a note

User Contributed Notes 1 note

edible dot email at gmail dot com
4 years ago
The notes on this function are not very clear and a little misleading.

Firstly, <=5.3, you will need to make use of one of several scripts or classes available on the internet which might, or might not, require the installation of of the intl and idn PECL extensions ...and you will need to have !<4.0 in order to be able to install both.

Secondly, if you have >=5.4 you will not require the PECL extensions.

Third, use of utf8_encode() is not necessary.  In fact, it will potentially prevent idn_to_ascii() from working at all.

On my setup it was necessary to change the charset in the script meta tags to UTF-8:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

...and to change charset_default in the php.ini file (/usr/local/lib/php.ini, whereis php.ini, find / -name php.ini):

default_charset = "UTF-8"

The above changes mean that idn_to_ascii() can now be used with that syntax (no need for utf8_encode()).  Previously, the function worked to convert some IDNs, but failed to convert Japanese and Cyrillic IDNs.  Further, no additional locales were enabled or added, and Apache's charset file was left unmodified.

It is also important to remember only to apply the function where required, eg:

idn_to_ascii(cå // is wrong


iden_to_ascii(cåsino) // is right

...and also be aware of text editors that don't support UTF-8 encoding, or the $domain = 'cåsino' value will end up as $domain = '??????' ...and the function will fail.

I have found that Notepad++ easily and reliably handles UTF-8 encoding that works for this function using UTF-8 as the encoding option, not UTF-8 without BOM.
To Top