UTF-16にエンコードされている文字列をUTF-8へデコード
2024/03/04
エンコードされている文字列であまり見かけない感じのものがあった。UTF-8やShift-JIS、EUC-JPではなく、色々調べるとUTF-16でエンコードされていた。以下でUTF-8にデコードできた。
ソースコード
<?php
$body = '\u30c6\u30b9\u30c8';
$decoded = preg_replace_callback(
'|\\\\u([0-9a-f]{4})|i',
function($matched){
return mb_convert_encoding(pack('H*', $matched[1]), 'UTF-8', 'UTF-16');
},
$body
);
echo $decoded;//テストと表示される。
参考サイト
関連記事
-
-
PHPでCannot re-assign auto……のエラー
PHPバージョンアップに伴い「Fatal error: Cannot re-as ...
-
-
PHPでGoogle翻訳を手軽に扱えるライブラリ「google-translate-php」の利用方法
サイト内で一部テキストのGoogle翻訳を行いたい。ただ、Google Clou ...
-
-
PHP8系で「Warning: Constant xxxxx already defined in」エラーの対応方法
PHP8系&WordPress6.4.3で「Warning: Const ...
-
-
PHPからDBX Platformを利用してDropbox内にディレクトリ(フォルダ)を作成する方法
PHPからDBX Platformを利用してDropbox内にディレクトリ(フォ ...
-
-
PHPで「operator not supported for strings」エラーの対応方法
PHPにて「Fatal error: [] operator not suppo ...