(PHP 5)
Décode une chaine encodée en Punnycode.
idn_decode() retourne la chaîne input décodée selon l'algorithme défini dans la RFC 3492.
Cette fonction est expérimentale. Bien qu'aucun problème n'ai été constaté à ce jour, il n'est pas impossible qu'elle puisse fournir des résultats inattendus.
<?php
function idn_decode( $input , $encoding = 'UTF-8' )
{
// Diverses "constantes"
$cPrefix = 'xn--';
$cBase = 36;
$cTmin = 1;
$cTmax = 26;
$cSkew = 38;
$cDamp = 700;
$cInitial_bias = 72;
$cInitial_n = 0x80;
$cDigits = 'abcdefghijklmnopqrstuvwxyz0123456789';
// Diverses fonctions
$fAdapt = create_function('$delta, $numpoints, $firstime',
'
$cBase = '. $cBase .';
$cTmin = '. $cTmin .';
$cTmax = '. $cTmax .';
$cSkew = '. $cSkew .';
$cDamp = '. $cDamp .';
if($firstime)
$delta = $delta / $cDamp;
else
$delta = $delta / 2;
$delta += $delta / $numpoints;
$k = 0;
while( $delta > (($cBase - $cTmin) * $cTmax) / 2 )
{
$delta = $delta / ($cBase - $cTmin);
$k += $cBase;
}
return $k + ($cBase - $cTmin + 1) * $delta / ($delta + $cSkew);
');
$fDecodeDigit = create_function('$digit',
'
$cDigits = "'. $cDigits .'";
return strpos($cDigits, strtolower($digit));
');
$fUnichr = create_function('$unicode, $encoding="UTF-8"',
'
$unicode = intval($unicode);
return mb_convert_encoding("&#{$unicode};", $encoding, "HTML-ENTITIES");
');
// C'est parti !
$output = $input;
preg_match_all(
'`('. preg_quote($cPrefix,'`') .')(([-a-z0-9]+)-)?([a-z0-9]+)`',
$input,
$matches
);
for($mi=0; $mi<count($matches[0]); $mi++)
{
// Boucle sur les différentes chaines encodées
$encoded = $matches[0][$mi];
if( strlen($matches[3][$mi]) )
$decoded = array_map('ord', str_split($matches[3][$mi]));
else
$decoded = array();
$code = $matches[4][$mi];
$encoded_lenght = strlen($encoded);
$decoded_lenght = count($decoded);
$code_length = strlen($code);
$n = $cInitial_n;
$i = 0;
$bias = $cInitial_bias;
for($iCode=0; $iCode<$code_length; ++$decoded_lenght)
{
$oldi = $i;
$w = 1;
for ($k = $cBase; ; $k += $cBase)
{
$digit = $fDecodeDigit( $code{$iCode} );
$iCode++;
$i += $digit * $w;
if( $k <= $bias )
$t = $cTmin;
elseif( $k >= $bias + $cTmax )
$t = $cTmax;
else
$t = $k - $bias;
if($digit < $t) break;
$w = intval($w * ( $cBase - $t));
}
$bias = $fAdapt($i - $oldi, $decoded_lenght + 1, ($oldi==0));
$n += intval($i / ($decoded_lenght + 1));
$i %= ($decoded_lenght + 1);
// Insère le caractère au bon endroit
array_splice(
$decoded, $i, count($decoded),
array_merge(array($n), array_slice($decoded, $i, count($decoded)))
);
$i++;
}
$decoded_string = '';
foreach($decoded as $decoded_char)
$decoded_string .= $fUnichr($decoded_char, $encoding);
$output = str_replace($encoded, $decoded_string, $output);
}
return $output;
}
?>
string idn_decode ( string $input [, string $encoding = 'UTF-8' ] )
Retourne la chaîne décodée.
Exemple #1 Exemple avec idn_decode()
<?php $str = "www.xn--pre-nol-2xa6a.net"; $str = idn_decode($str); echo $str; // www.père-noël.net ?>
Exemple #2 Exemple avec idn_decode()
<?php $str = "user@xn--champs-lyses-hebd.com"; $str = idn_decode($str, "ISO-8859-1"); echo $str; // user@champs-élysées.com (en ISO) ?>
Commentaire(s)
Poster un commentaire