SunshinePHP Developer Conference 2015

idn_to_ascii

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

idn_to_asciiConvierte un nombre de dominio a formato IDNA ASCII

Descripción

Estilo por procedimientos

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

Esta función convierte un dominio Unicode al formato IDNA ASCII-compatible.

Parámetros

domain

Dominio a convertir. En PHP 5 debe estar previamente codificado en UTF-8.

options

Opciones de conversión - combinación de constantes IDNA_* (excepto las constantes IDNA_ERROR_*).

variant

INTL_IDNA_VARIANT_2003 para IDNA 2003 o INTL_IDNA_VARIANT_UTS46 para UTS #46.

idna_info

Este parámetro sólo se puede usar si INTL_IDNA_VARIANT_UTS46 se utilizó para variant. En este caso, será llenado con un array con las claves 'result', la posibilidad de un resultado ilegal de la transformación, 'isTransitionalDifferent', un booleano indicando si el uso de los mecanismos de transición de UTS #46 ha cambiado o debería haber cambiado el resulado, y 'errors', que es de tipo int y representa un conjunto de bits de las constantes de error IDNA_ERROR_*.

Valores devueltos

Nombre del dominio codificado en formato ASCII-compatible. o FALSE en caso de error

Historial de cambios

Versión Descripción
5.4.0/PECL 2.0.0b1 Se añadieron los parámetros variant y idna_info; y el soporte para UTS #46.

Ejemplos

Ejemplo #1 Ejemplo de idn_to_ascii()

<?php

echo idn_to_ascii('täst.de'); 

?>

El resultado del ejemplo sería:

xn--tst-qla.de

Ver también

add a note add a note

User Contributed Notes 2 notes

up
1
edible dot email at gmail dot com
2 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åsino.com) // is wrong

...whereas...

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.
up
0
Eman
3 years ago
With PHP 5.3 I started to recieve

Fatal error: Only variables can be passed by reference

when trying to call idn_to_ascii with second optional parameter (IDNA_DEFAULT).
Moreover these IDNA_* constants are seem to be undefined in PHP 5.3. + PECL idn 0.0.2

When I tried to uninstall PECL idn and install PECL intl (1.1.1) i recieved this:

/tmp/pear/temp/intl/collator/collator_class.c:92: error: duplicate ‘static’
/tmp/pear/temp/intl/collator/collator_class.c:96: error: duplicate ‘static’
/tmp/pear/temp/intl/collator/collator_class.c:101: error: duplicate ‘static’
/tmp/pear/temp/intl/collator/collator_class.c:107: error: duplicate ‘static’
make: *** [collator/collator_class.lo] Error 1

So probably compiling PHP 5.3. from sources with --enable-intl will help, but it only means that PHP 5.3. intl version differs from the one available at PECL, which isn't good, I think.

I've made my code working again by giving second idn_to_ascii parameter as simple integer variable (with value 0), but something bad is going on here and i will not be surprised if it'll break again after another PHP update...
To Top