Domanda

Qualcuno può aiutarmi ?come posso ottenere caratteri UCS2/HexEncoded

come 'Ciao' restituirà "00480065006C006C006F"

Questi sono i valori HexEncoded:

0048 = H 0065 = E 006C = L 006C = L 006F = O*

Anche in arabo (!مرحبا عالم) restituirà 06450631062d0628064b06270020063906270644064500200021

Come posso ottenere l'UCS2 codificato in php?

È stato utile?

Soluzione

mb_convert_encoding($str, 'UCS-2', 'auto') funziona correttamente per convertire la stringa, ma dovrai fare del lavoro extra per ottenere l'output corretto in un browser.

Dovrai modificare il set di caratteri del tuo output in modo che corrisponda a UCS-2 per poterlo utilizzare eco per visualizzarlo su una pagina.Inoltre, potrebbe essere necessario impostare il tipo di contenuto tramite a meta tag anche nell'intestazione.

Ho incluso tre esempi qui nelle seguenti varianti Unicode:UCS-2, UTF-16 e UTF-8;poiché non tutti hanno funzionato per me senza modificare Internet Explorer.Potrebbe essere necessario archiviare i file PHP in UTF-8 per ottenere risultati corretti.Inoltre, utilizzo una versione inglese di Windows, quindi non posso inserire la stringa araba nel formato RTL corretto.Mi dispiace se la tua stringa è confusa qui.Ti assicuro che se lo sostituisci nella posizione indicata dai miei commenti, otterrai il risultato corretto.Infine, potresti avere problemi a visualizzare UCS-2 e UTF-16 in Internet Explorer: sembrano esserci alcune stranezze quando l'output viene ricaricato tramite una cache.Tuttavia, FireFox 3.5.5 ha funzionato per tutte e tre le codifiche.Se sei seriamente intenzionato a creare un'app, ti consiglio vivamente di prendere in considerazione l'utilizzo di UTF-8 anziché UCS-2.

Versione UCS-2

FireFox 3.5.5 (Ok, ma FireFox dice che è UTF-16BE nel mio test.)
Internet Explorer 7.0 (non ok.Non è stato rilevato/convertito correttamente l'arabo.)

<?php
header('Content-Type: text/html; charset=UCS-2');
mb_http_output('UCS-2');
echo mb_convert_encoding('<html><head><meta http-equiv="Content-Type" content="text/html; charset=UCS-2" /></head><body>', 'UCS-2', 'auto');
echo mb_convert_encoding('encoding: ', 'UCS-2', 'auto');
echo mb_convert_encoding(mb_http_output(), 'UCS-2', 'auto');
echo mb_convert_encoding('<br />', 'UCS-2', 'auto');
// NOTE: Replace the string here with your phrase
$strTerm = '!مرحبا عالم';
echo mb_convert_encoding('$strTerm = '.$strTerm.'<br />', 'UCS-2', 'auto');
echo mb_convert_encoding('query string: '.$_SERVER['QUERY_STRING'].'<br />', 'UCS-2', 'auto');
echo mb_convert_encoding('original hex: '.bin2hex($strTerm).'<br />', 'UCS-2', 'auto');
echo mb_convert_encoding('transformed hex: '.bin2hex(mb_convert_encoding($strTerm, 'UCS-2', 'auto')).'<br />', 'UCS-2', 'auto');
echo mb_convert_encoding('</body>', 'UCS-2', 'auto');
?>

Versione UTF-16

FireFox 3.5.5 (100% ok)
Internet Explorer 7.0 (non riuscito.Potrebbe essere necessario specificare l'ordine dei byte.)

<?php
header('Content-Type: text/html; charset=UTF-16');
mb_http_output('UTF-16');
echo mb_convert_encoding('<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-16" /></head><body>', 'UTF-16', 'auto');
echo mb_convert_encoding('encoding: ', 'UTF-16', 'auto');
echo mb_convert_encoding(mb_http_output(), 'UTF-16', 'auto');
echo mb_convert_encoding('<br />', 'UTF-16', 'auto');
// NOTE: Replace the string here with your phrase
$strTerm = '!مرحبا عالم';
echo mb_convert_encoding('$strTerm = '.$strTerm.'<br />', 'UTF-16', 'auto');
echo mb_convert_encoding('query string: '.$_SERVER['QUERY_STRING'].'<br />', 'UTF-16', 'auto');
echo mb_convert_encoding('original hex: '.bin2hex($strTerm).'<br />', 'UTF-16', 'auto');
echo mb_convert_encoding('transformed hex: '.bin2hex(mb_convert_encoding($strTerm, 'UTF-16', 'auto')).'<br />', 'UTF-16', 'auto');
echo mb_convert_encoding('</body>', 'UTF-16', 'auto');
?>

UTF-8

FireFox 3.5.5 (100% ok)
Internet Explorer 7.0 (100% ok)

<?php
header('Content-Type: text/html; charset=UTF-8');
mb_http_output('UTF-8');
echo mb_convert_encoding('<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>', 'UTF-8', 'auto');
echo mb_convert_encoding('encoding: ', 'UTF-8', 'auto');
echo mb_convert_encoding(mb_http_output(), 'UTF-8', 'auto');
echo mb_convert_encoding('<br />', 'UTF-8', 'auto');
// NOTE: Replace the string here with your phrase
$strTerm = '!مرحبا عالم';
echo mb_convert_encoding('$strTerm = '.$strTerm.'<br />', 'UTF-8', 'auto');
echo mb_convert_encoding('query string: '.$_SERVER['QUERY_STRING'].'<br />', 'UTF-8', 'auto');
echo mb_convert_encoding('original hex: '.bin2hex($strTerm).'<br />', 'UTF-8', 'auto');
echo mb_convert_encoding('transformed hex: '.bin2hex(mb_convert_encoding($strTerm, 'UTF-8', 'auto')).'<br />', 'UTF-8', 'auto');
echo mb_convert_encoding('</body>', 'UTF-8', 'auto');
?>

Altri suggerimenti

questa pagina web , il Multi modulo byte String (mbstring) supporta UCS-2. Dopo aver attivato questo modulo, è possibile utilizzare la funzione di mb_convert_encoding per convertire una stringa da una codifica all'altra.

della funzione mb_convert_encoding :

string mb_convert_encoding  ( string $str  , string $to_encoding  [, mixed $from_encoding  ] )
Converts the character encoding of string str to to_encoding from optionally from_encoding . 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top