Created
August 26, 2021 16:34
-
-
Save nczz/16ec375e10c56858abf71e605387a20e to your computer and use it in GitHub Desktop.
Facebook 匯出 JSON 檔案編碼修正
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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