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 ;)
unicode_encode
(PHP 6 >= 6.0.0)
unicode_encode — Convert a unicode string in any encoding
설명
Takes a unicode string and converts it to a string in the specified encoding .
인수
- 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.
반환값
A string on success, or FALSE on failure.
오류/예외
Emits a E_WARNING level error if a converter cannot be created for the desired encoding .
예제
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 'The string itself:', $encoded, PHP_EOL;
echo 'The length of the string: ', strlen ($encoded);
?>
위 예제의 출력 예시:
The string itself: ŐŹ The length of the string: 2
주의
이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.
참고
- unicode_set_error_mode() - Set the error mode for strings conversions
- unicode_decode() - Convert a binary string into a Unicode string
unicode_encode
18-Oct-2009 12:17
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;
}
?>
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
