PHP IDN Punycode Converter


The domain name system worked well enough for english, but as the world becomes more connected to the internet, the more people want to use it without typing in domain names using the latin alphabet.

In order to make domain names made up of foreign characters backwards compatible with our existing system, and alternate representation, sometimes called Punycode was defined in RFC3492.

For example the russian domain
пустыня.com
can be converted to a latin character safe version:
xn--m1adged4c3a.com

Example PHP code for converting to and from punycode IDNs:
<?php
include_once("punycode.class.php")
echo Punycode::encodeHostname('bücher.ch')."\n";//outputs xn--bcher-kva.ch
echo Punycode::decodeHostname('xn--bcher-kva.ch')."\n";//outputs bücher.ch
//Also japanese
echo Punycode::encodeHostname('ジェーピーニック.jp')."\n";//xn--hckqz9bzb1cyrb.jp
echo Punycode::decodeHostname('xn--hckqz9bzb1cyrb.jp')."\n";//ジェーピーニック.jp
//Also TLDs
echo Punycode::encodeHostname('www.台灣')."\n";//www.xn--kpry57d
echo Punycode::decodeHostname('www.xn--kpry57d')."\n";//www.台灣
?>
Links:
* Download punycode.class.php
* GitHub Project Page PHP IDN Library


code snippets are licensed under Creative Commons CC-By-SA 3.0 (unless otherwise specified)