downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

unicode_get_error_mode> <unicode_decode
Last updated: Fri, 14 Aug 2009

view this page in

unicode_encode

(PHP 6 >= 6.0.0)

unicode_encodeDéfinit l'encodage d'une chaîne unicode

Description

string unicode_encode ( unicode $input , string $encoding [, int $errmode ] )

Prend une chaîne unicode et la convertie en une chaîne dans un encodage spécifié.

Liste de paramètres

input

La chaîne unicode qui va être encodée.

encoding

Le nouvel encodage de input .

errmode

Mode d'erreur de conversion. Ce paramètre détermine l'action à effectuer lorsque le convertisseur n'a pu convertir un caractère. Pour une liste des modes disponibles, référez-vous à la fonction unicode_set_error_mode(). Si ce paramètre n'est pas défini, le mode d'erreur global sera utilisé.

Valeurs de retour

Une chaîne de caractères en cas de succès, ou FALSE si une erreur survient.

Erreurs / Exceptions

Émet une erreur de type E_WARNING si le convertisseur n'a pu être créé pour l'encodage encoding désiré.

Exemples

Exemple #1 Exemple avec unicode_encode()

Note: Les caractères seront visibles au lieu des entités dans l'affichage.

<?php
header 
('Content-Type: text/plain; charset=ISO-8859-2');

$encoded unicode_encode ('\u0150\u0179''ISO-8859-2');

echo 
'La chaîne elle-même :'$encodedPHP_EOL;
echo 
'La longueur de la chaîne : 'strlen ($encoded);
?>

L'exemple ci-dessus va afficher quelque chose de similaire à :

La chaîne elle-même : &#336;&#377;
La longueur de la chaîne : 2

Notes

Avertissement

Cette fonction est EXPERIMENTALE. Cela signifie que le comportement de cette fonction, son nom et, concrètement, TOUT ce qui est documenté ici peut changer dans un futur proche, SANS PREAVIS ! Soyez-en conscient, et utilisez cette fonction à vos risques et périls.

Voir aussi



unicode_get_error_mode> <unicode_decode
Last updated: Fri, 14 Aug 2009
 
add a note add a note User Contributed Notes
unicode_encode
Nicolae Namolovan
18-Oct-2009 12:17
Want to display the acutal character represented by Unicode Code Annotation U+.. for example U+00CE ?

The unicode_to_utf8 you find in comments futher will help us, but it requires as argument an array of integers, 00CE in our example (and the unicode notation) is hex, everything we need to do is to apply the hexdec function to convert it to decimal.

<?php echo unicode_to_utf8(array(hexdec("00CE"))); // Result: Î ?>

Or a handy function that will take U+00CE and will make the same thing

<?php
function unicodeCodePointToChar($str) {
    if (
substr($str,0,2) != 'U+') return $str;
   
$str = substr($str,2); // Skip U+
   
return unicode_to_utf8(array(hexdec($str)));
}

echo
unicodeCodePointToChar("U+00CE"); // Result: Î
?>

Hopefully that will help someone like me ;)
berlioz at nicematin dot fr
14-Dec-2007 01:37
a little usefull function

<?php
function unicode2utf8($c)
{
   
$output="";
   
    if(
$c < 0x80)
    {
        return
chr($c);
    }
    else if(
$c < 0x800)
    {
        return
chr( 0xc0 | ($c >> 6) ).chr( 0x80 | ($c & 0x3f) );
    }
    else if(
$c < 0x10000)
    {
        return
chr( 0xe0 | ($c >> 12) ).chr( 0x80 | (($c >> 6) & 0x3f) ).chr( 0x80 | ($c & 0x3f) );
    }
    else if(
$c < 0x200000)
    {
        return
chr(0xf0 | ($c >> 18)).chr(0x80 | (($c >> 12) & 0x3f)).chr(0x80 | (($c >> 6) & 0x3f)).chr(0x80 | ($c & 0x3f));
    }
    return
false;
}
?>
spam at or-k dot de
22-Feb-2007 04:45
A PHP4 Version for converting a utf8 string/text to unicode:

    function utf8_to_unicode( $str ) {
     
        $unicode = array();       
        $values = array();
        $lookingFor = 1;
       
        for ($i = 0; $i < strlen( $str ); $i++ ) {
            $thisValue = ord( $str[ $i ] );
        if ( $thisValue < ord('A') ) {
            // exclude 0-9
            if ($thisValue >= ord('0') && $thisValue <= ord('9')) {
                 // number
                 $unicode[] = chr($thisValue);
            }
            else {
                 $unicode[] = '%'.dechex($thisValue);
            }
        } else {
              if ( $thisValue < 128)
            $unicode[] = $str[ $i ];
              else {
                    if ( count( $values ) == 0 ) $lookingFor = ( $thisValue < 224 ) ? 2 : 3;               
                    $values[] = $thisValue;               
                    if ( count( $values ) == $lookingFor ) {
                        $number = ( $lookingFor == 3 ) ?
                            ( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 ):
                            ( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64 );
                $number = dechex($number);
                $unicode[] = (strlen($number)==3)?"%u0".$number:"%u".$number;
                        $values = array();
                        $lookingFor = 1;
              } // if
            } // if
        }
        } // for
        return implode("",$unicode);

    } // utf8_to_unicode

unicode_get_error_mode> <unicode_decode
Last updated: Fri, 14 Aug 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites