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;
}
?>
unicode_encode
(No version information available, might be only in CVS)
unicode_encode — unicode 文字列を任意のエンコーディングに変換する
説明
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 'Unicode semantics: ', ini_get ('unicode_semantics'), PHP_EOL;
echo 'The string itself:', $encoded, PHP_EOL;
echo 'The length of the string: ', strlen ($encoded);
?>
上の例の出力は、たとえば 以下のようになります。
Unicode semantics: 1 The string itself: ŐŹ The length of the string: 2
注意
警告
この関数は、 実験的 なものです。この関数の動作・ 名前・その他ドキュメントに書かれている事項は、予告なく、将来的な PHP のリリースにおいて変更される可能性があります。 この関数は自己責任で使用してください。
unicode_encode
berlioz at nicematin dot fr
14-Dec-2007 05:37
14-Dec-2007 05:37
spam at or-k dot de
22-Feb-2007 08:45
22-Feb-2007 08: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
