Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
PHP – idn_to_ascii() function
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 non-ASCII characters.
Syntax
string idn_to_ascii( string $domain, int $flags = IDNA_DEFAULT, int $variant = INTL_IDNA_VARIANT_UTS46, array &$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
Basic usage of converting a Unicode domain to ASCII form ?
<?php
// String domain with umlaut character
echo idn_to_ascii('täst.de', 0);
?>
xn--tst-qla.de
Example 2
Converting UTF-8 encoded domain names and handling edge cases ?
<?php
// Encoded string with UTF-8 encoding
echo idn_to_ascii(utf8_encode('täst.de')) . "<br>";
// Test with another Unicode domain
echo idn_to_ascii('müller.com') . "<br>";
// Example with Cyrillic characters
echo idn_to_ascii('??????.test');
?>
xn--tst-fea82a.de xn--mller-kva.com xn--e1afmkfd.test
Using Different Variants
Demonstrating the use of different IDNA variants ?
<?php $domain = 'münchen.de'; // Using UTS46 variant (default) echo "UTS46: " . idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46) . "<br>"; // Using IDNA 2003 variant echo "IDNA 2003: " . idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_2003); ?>
UTS46: xn--mnchen-3ya.de IDNA 2003: xn--mnchen-3ya.de
Conclusion
The idn_to_ascii() function is essential for converting internationalized domain names to ASCII-compatible encoding. It ensures proper handling of Unicode characters in domain names for web applications that need ASCII-only domain representation.
