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;//テストと表示される。
参考サイト
関連記事
-
-
PHP8系で「Warning: Attempt to read property "xxxxxx" 」エラーの対応方法
PHP8系&WordPress6.4.3で「Warning: Attem ...
-
-
PHPのsetcookieで「Cannot modify」エラーの対応方法
PHPでsetcookieを使うと「Warning: Cannot modify ...
-
-
PHPからLINEのMessaging APIにリクエストし通知を送る方法
LINEから「2025/3/31にLINE Notifyのサービスが終了する」と ...
-
-
Composerで後から別のパッケージを追加する方法と削除する方法
Composerで何らかのパッケージをインストールし、後から別のパッケージを追加 ...
-
-
PHPで配列じゃないものに対してソート
PHP Warning: sort() expects parameter 1 ...