Skip to content

Instantly share code, notes, and snippets.

View m-macdev's full-sized avatar

Mac Dev m-macdev

View GitHub Profile
@droduit
droduit / fix-serialization.php
Last active November 30, 2025 13:26
Fix serialized data broken due to search & replace in database
<?php
/*
* The `fix_serialized` function fixes serialized data which has been corrupted by an incorrect byte count length,
* especially in the case of a search & replace done on a dump of a wordpress database.
*/
$corruptedSerialization = 'a:3:{i:0;s:150:".DS_Store";i:1;s:100:".git";i:2;s:190:"node_modules";}';
if (is_serialized($corruptedSerialization)) {
$fixed = fix_serialized($corruptedSerialization);
print_r(unserialize($fixed));
}