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, 13 Nov 2009

view this page in

unicode_encode

(PHP 6 >= 6.0.0)

unicode_encodeunicode 文字列を任意のエンコーディングに変換する

説明

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

unicode 文字列を受け取り、それを指定したエンコーディング encoding の文字列に変換します。

パラメータ

input

エンコードする unicode 文字列。

encoding

input の新しいエンコーディング。

errmode

変換エラーモード。このパラメータで、 コンバータが文字を変換できなかったときの動作を指定します。 使用できるモードについては unicode_set_error_mode() を参照ください。このパラメータを省略した場合は、グローバルなエラーモードを使用します。

返り値

成功した場合に文字列、失敗した場合に FALSE を返します。

エラー / 例外

指定したエンコーディングの変換器が作成できない場合に E_WARNING レベルのエラーが発生します。

例1 unicode_encode() の例

注意: 出力は、エンティティではなく文字になります。

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

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

echo 
'The string itself:'$encodedPHP_EOL;
echo 
'The length of the string: 'strlen ($encoded);
?>

上の例の出力は、 たとえば以下のようになります。

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

注意

警告

この関数は、 実験的 なものです。この関数の動作・ 名前・その他ドキュメントに書かれている事項は、予告なく、将来的な PHP のリリースにおいて変更される可能性があります。 この関数は自己責任で使用してください。

参考



unicode_get_error_mode> <unicode_decode
Last updated: Fri, 13 Nov 2009
 
add a note add a note User Contributed Notes
unicode_encode
Nicolae Namolovan
18-Oct-2009 09: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 10: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
23-Feb-2007 01: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, 13 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites