Skip to content

Instantly share code, notes, and snippets.

@HDDen
Created February 7, 2024 08:51
Show Gist options
  • Save HDDen/cde4cbfdbb0eaabfa00fe8de37ac4e56 to your computer and use it in GitHub Desktop.
Save HDDen/cde4cbfdbb0eaabfa00fe8de37ac4e56 to your computer and use it in GitHub Desktop.
Fix broken serialized data (drupal metatags, for example)
<?php
// based on https://stackoverflow.com/a/21389439/18216946
$unserialized = "a:2:{s:2:"aaaaaaaaaaaaa";}"; // some potentially broken data
$fixed_data = preg_replace_callback ( '!s:(\d+):"(.*?)";!', function($match) {
return ($match[1] == strlen($match[2])) ? $match[0] : 's:' . strlen($match[2]) . ':"' . $match[2] . '";';
},$unserialized );
$final = unserialize($fixed_data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment