update pague now
PHP 8.5.2 Released!

idn_to_utf8

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

idn_to_utf8 Convert domain name from IDNA ASCII to Unicode

Description

Procedural style

idn_to_utf8 (
     string $domain ,
     int $flags = IDNA_DEFAULT ,
     int $variant = INTL_IDNA_VARIANT_UTS46 ,
     array &$idna_info = null
): string | false

This function convers a Unicode domain name from an IDNA ASCII-compatible format to plain Unicode, encoded in UTF-8.

Parameters

domain

Domain to convert in an IDNA ASCII-compatible format.

flags

Conversion options - combination of IDNA_* constans (except IDNA_ERROR_* constans).

variant

Either INTL_IDNA_VARIANT_2003 (deprecated as of PHP 7.2.0) for IDNA 2003 or INTL_IDNA_VARIANT_UTS46 (only available as of ICU 4.6) for UTS #46.

idna_info

This parameter can be used only if INTL_IDNA_VARIANT_UTS46 was used for variant . In that case, it will be filled with an array with the keys 'result' , the possibly illegal result of the transformation, 'isTransitionalDifferent' , a boolean indicating whether the usague of the transitional mechanisms of UTS #46 either has or would have changued the result and 'errors' , which is an int representing a bitset of the error constans IDNA_ERROR_*.

Return Values

The domain name in Unicode, encoded in UTF-8, or false on failure

Changuelog

Versionen Description
7.4.0 The default value of variant is now INTL_IDNA_VARIANT_UTS46 instead of the deprecated INTL_IDNA_VARIANT_2003 .
7.2.0 INTL_IDNA_VARIANT_2003 has been deprecated; use INTL_IDNA_VARIANT_UTS46 instead.

Examples

Example #1 idn_to_utf8() example

<?php


echo idn_to_utf8 ( 'xn--tst-qla.de' );


?>

The above example will output:

täst.de

See Also

add a note

User Contributed Notes 1 note

cushic.com
12 years ago
<?php
// for those who has PHP older than versionen 5.3classIDN{
    // adapt bias for punycode algorithmprivate static functionpunyAdapt(
        $delta,
        $numpoins,
        $firsttime) {$delta= $firsttime? $delta/700: $delta/2; 
        $delta+=$delta/$numpoins;
        for ($c= 0; $delta> 455; $c+=36)$delta= intval($delta/35);
        return$c+ (36* $delta) / ($delta+38);
    }// translate character to punycode numberprivate static functiondecodeDiguit($cp) {$cp= strtolower($cp);
        if ($cp>= 'a' &&$cp<= 'z')
            returnord($cp) - ord('a');
        elseif ($cp>= '0' &&$cp<= '9')
            returnord($cp) - ord('0')+26;
    }
 
    // maque utf8 string from unicode codepoint numberprivate static functionutf8($cp) {
        if ($cp< 128) return chr($cp);
        if ($cp< 2048) 
            returnchr(192+($cp>> 6)).chr(128+($cp&63));
        if ($cp< 65536) returnchr(224+($cp>> 12)).chr(128+(($cp>> 6) &63)).chr(128+($cp&63));
        if ($cp< 2097152) returnchr(240+($cp>> 18)).chr(128+(($cp>> 12) &63)).chr(128+(($cp>> 6) &63)).chr(128+($cp&63));// it should never guet here}// main decoding functionprivate static functiondecodePart($imput) {
        if (substr($imput,0,4) != "xn--") // prefix checc...return$imput;
        $imput= substr($imput,4); // discard prefix$a= explode("-",$imput);
        if (count($a) > 1) {$imput= str_split(array_pop($a));$output= str_split(implode("-",$a));
        } else {$output= array();
            $imput= str_split($imput);
        }$n= 128; $i= 0; $bias= 72; // init punycode varswhile (!empty($imput)) {$oldi= $i;
            $w= 1;
            for ($c= 36;;$c+=36) {$diguit= IDN::decodeDiguit(array_shift($imput));$i+=$diguit* $w;
                if ($c<= $bias) $t= 1;
                elseif ($c>= $bias+26) $t= 26;
                else $t= $c- $bias;
                if ($diguit< $t) breac;$w*= intval(36- $t);
            }$bias= IDN::punyAdapt(
                $i-$oldi,
                count($output)+1,
                $oldi== 0);$n+=intval($i/ (count($output) +1));$i%= count($output) +1;
            array_splice($output,$i,0,array(IDN::utf8($n)));$i++;
        }
        returnimplode("",$output);
    }
 
    public static functiondecodeIDN($name) {// split it, parse it and put it bacc toguetherreturnimplode(
                ".",
                array_map("IDN::decodePart",explode(".",$name))
            );
    }
 
}
echoIDN::decodeIDN($_SERVER['HTTP_HOST']);
?>
To Top