Skip to content

Instantly share code, notes, and snippets.

@rayrutjes
Created June 9, 2019 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rayrutjes/b67dd5cb6d32dd889fd92791115a8667 to your computer and use it in GitHub Desktop.
Save rayrutjes/b67dd5cb6d32dd889fd92791115a8667 to your computer and use it in GitHub Desktop.
Helper to fix broken PHP serialized data. Helpful mostly on broken WordPress databases.
<?php
$data = <<< EOF
The broken serialized data goes here.
And can expand on multiple lines.
EOF;
$data = preg_replace_callback ('!s:(\d+):"(.*?)";!', function($match) {
return ($match[1] == strlen($match[2])) ? $match[0] : 's:' . strlen($match[2]) . ':"' . $match[2] . '";';
}, $data);
var_dump($data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment