PHP – idn_to_ascii() function

PHPServer Side ProgrammingProgramming

The idn_to_ascii() function in PHP is used to convert a Unicode domain name into IDNA ASCII form. IDNA stands for Internationalizing Domain Names in Applications. It is a mechanism for handling internationalized domain names containing nonASCII characters.

Syntax

string idn_to_ascii(
   str $domain,
   integer $flags=IDNA_DEFAULT,
   integer $variant=INTL_IDNA_VARIANT_UTS46,
   arr &$idna_info=null
)

Parameters

idn_to_ascii() accepts the following four parameters −

  • $domain − This is the domain to be converted; it must be UTF-8 encoded.

  • $flags − This parameter is a combination of IDNA_*constants.

  • $variant − This parameter uses either INTL_IDNA_VARIANT_2003 for IDNA 2003 or INTL_IDNA_VARIANT_UTS46 for UTS#46.

  • $idna_info − This parameter is used only if the INTL_IDNA_VARIANT_UTS46 is used in the $variant parameter.

Return Values

This function returns the domain name encoded in ASCII-compatible form, or it returns False on failure.

Example 1

<?php
   // String domain
   print idn_to_ascii('täst.de',0);
?>

Output

xn--tst-qla.de

Example 2

<?php
   // encoded string ISO-8859-2
   echo idn_to_ascii(utf8_encode('täst.de'));

   // It cannot convert a domain name to ASCII
   // that contains non-ASCII chars but
   // it already start with "xn--"
   $ascii = idn_to_ascii("xn--".chr(0xC3).chr(0xA4));
   print_r($ascii);
?>

Output

xn--tst-fea82a.de
raja
Updated on 12-Oct-2021 06:50:44

Advertisements