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
Descrierea
Takes a unicode string and converts it to a string in the specified encoding .
Parametri
- 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.
Valorile întroarse
A string on success sau FALSE în cazul eşecului.
Erori/Excepţii
Emits a E_WARNING level error if a converter cannot be created for the desired encoding .
Exemple
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);
?>
Exemplul de mai sus va afişa ceva similar cu:
The string itself: ŐŹ The length of the string: 2
Note
Această funcţie este EXPERIMENTALĂ. Comportamentul acestei funcţii, denumirea sa şi orice alte aspecte documentate în privinţa acestei funcţii pot să fie modificate fără preaviz într-o versiune viitoare a PHP. Utilizaţi această funcţie la propriul risc.
Vedeţi de asemenea
- 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
