mb_convert_encoding($ str、'UCS-2'、'auto')は文字列を変換するために正しく機能しますが、ブラウザーで適切な出力を取得するには追加の作業を行う必要があります。
echoを使用してページに出力できるようにするには、出力の文字セットをUCS-2と一致するように変更する必要があります。また、ヘッダーのメタタグを介してContent-Typeを設定する必要がある場合もあります。
ここでは、次のユニコードバリアントに3つの例を含めました。UCS-2、UTF-16、およびUTF-8。それらのすべてがInternetExplorerを調整せずに私のために働いたわけではないので。適切な結果を得るには、PHPファイルをUTF-8に保存する必要がある場合があります。また、私は英語版のWindowsを使用しているため、適切なRTL形式でアラビア語の文字列を入力できません。ここで文字列が文字化けしてしまったらごめんなさい。私のコメントに記載されている場所で交換すると、適切な結果が得られることを保証します。最後に、Internet ExplorerでUCS-2とUTF-16を表示するのに問題があるかもしれません。出力がキャッシュを介してリロードされると、奇妙なことがあるようです。ただし、FireFox3.5.5は3つのエンコーディングすべてで機能しました。アプリの作成を真剣に考えている場合は、UCS-2ではなくUTF-8の使用を検討することを強くお勧めします。
UCS-2バージョン
FireFox 3.5.5(わかりました、しかしFireFoxは私のテストではUTF-16BEだと言っています。)
Internet Explorer 7.0(わかりません。アラビア語を正しく検出/変換しませんでした。)
<?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');
?>
UTF-16バージョン
FireFox 3.5.5(100%OK)
Internet Explorer 7.0(失敗。バイトオーダーを指定する必要がある場合があります。)
<?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');
?>