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

search for in the

unicode_get_error_mode> <unicode_decode
Last updated: Fri, 22 Aug 2008

view this page in

unicode_encode

(No version information available, might be only in CVS)

unicode_encodeConvert a unicode string in any encoding

Descripción

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

Takes a unicode string and converts it to a string in the specified encoding .

Lista de parámetros

input

The unicode string that is converted.

encoding

The new encoding for input .

errmode

Conversion error mode. This parameter determines the action to take when the converter cannot convert a character. For a list of available modes, refer to unicode_set_error_mode(). If the parameter is not set, the global error mode is used.

Valores retornados

A string on success, or FALSE on failure.

Errors/Exceptions

Emits a E_WARNING level error if a converter cannot be created for the desired encoding .

Ejemplos

Example #1 A unicode_encode() example

Note: The characters will be seen instead of entities in the output.

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

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

echo 
'Unicode semantics: 'ini_get ('unicode_semantics'), PHP_EOL;
echo 
'The string itself:'$encodedPHP_EOL;
echo 
'The length of the string: 'strlen ($encoded);
?>

El resultado del ejemplo seria algo similar a:

Unicode semantics: 1
The string itself: &#336;&#377;
The length of the string: 2

Notes

Warning

Esta función es EXPERIMENTAL. Esto significa que el comportamiento de esta función, el nombre de esta función y en definitiva TODO lo documentado sobre esta función, puede cambiar en una futura version de PHP SIN AVISO. La advertencia queda hecha, y utilizar esta extensión queda bajo su propia responsabilidad.

Ver también



add a note add a note User Contributed Notes
unicode_encode
berlioz at nicematin dot fr
14-Dec-2007 02: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 05: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, 22 Aug 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites