Skip to content

Instantly share code, notes, and snippets.

@nczz
Created August 26, 2021 16:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nczz/16ec375e10c56858abf71e605387a20e to your computer and use it in GitHub Desktop.
Save nczz/16ec375e10c56858abf71e605387a20e to your computer and use it in GitHub Desktop.
Facebook 匯出 JSON 檔案編碼修正
<?php
/**
** 使用方式:在下載回來解壓的最上層目錄,執行 php -f facebook_export_json_decode.php 會將目錄下所有 JSON 檔案都轉檔編碼過。
**/
function glob_recursive($pattern, $flags = 0) {
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
$files = array_merge($files, glob_recursive($dir . '/' . basename($pattern), $flags));
}
return $files;
}
// Ref: https://sorashi.github.io/fix-facebook-json-archive-encoding/
function replace_with_byte($match) {
return chr(hexdec($match[1]));
}
$results = glob_recursive('*.json');
foreach ($results as $key => $json) {
$input = file_get_contents($json);
$output = preg_replace_callback('/\\\\u00([a-f0-9]{2})/', 'replace_with_byte', $input);
file_put_contents($json . '_utf8.json', mb_convert_encoding($output, 'UTF-8', 'auto'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment