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 — Converte uma string unicode para qualquer codificação
Descrição
Recebe um string unicode e converte para uma string na codificação especificada por encoding .
Parâmetros
- input
-
A string unicode que será convertida.
- encoding
-
A nova codificação para input .
- errmode
-
Modo de conversão de erro. Este parâmetro determina a ação quando o conversor não converte um caractere. Para uma lista de modos disponíveis, consulte unicode_set_error_mode(). Se o parâmetro não é usado, o modo de erro global é usado.
Valor Retornado
Uma string em caso de sucesso , ou FALSE em caso de falha.
Erros
Emite um E_WARNING se o conversor não poder ser criado para a codificação desejada.
Exemplos
Exemplo #1 A unicode_encode() example
Aviso: Os caracteres, e não as entidades, serão vistos na saída.
<?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);
?>
O exemplo acima irá imprimir algo similar a:
Unicode semantics: 1 The string itself: ŐŹ The length of the string: 2
Notas
Esta função é EXPERIMENTAL. O comportamento desta função, seu nome, incluindo toda documentação pode ser modificado sem aviso em futuras versões do PHP. Esta função deve ser usada por sua própria conta e risco.
Veja Também
- unicode_set_error_mode() - Set the error mode for strings conversions
- unicode_semantics()
- unicode_decode() - Convert a binary string into a Unicode string
unicode_encode
18-Oct-2009 10:17
14-Dec-2007 11: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 02: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
