Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Unicode Functions : unicode_encode

unicode_encode

Convert a unicode string in any encoding ()
string unicode_encode ( unicode input, string encoding [, int errmode] )

Example 2562. 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:', $encoded, PHP_EOL;
echo
'The length of the string: ', strlen ($encoded);
?>

The above example will output something similar to:

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

Code Examples / Notes » unicode_encode

hugo dworak post

As for an example of the usage of the function unicode_encode:
<?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, 'The string itself: ';
 printf ($encoded . PHP_EOL, '%s');
 echo 'The length of the string: ', strlen ($encoded);
?>
The above example will output (please note that there will be characters instead of entities in the output):
Unicode semantics: 1
The string itself: &#336;&#377;
The length of the string: 2


spam

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


Change Language


Follow Navioo On Twitter
unicode_decode
unicode_encode
unicode_get_error_mode
unicode_get_subst_char
unicode_semantics
unicode_set_error_mode
unicode_set_subst_char
eXTReMe Tracker