Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Last active August 12, 2019 19:16
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 james2doyle/5260e1a05ed2a793ee411db54e8018e0 to your computer and use it in GitHub Desktop.
Save james2doyle/5260e1a05ed2a793ee411db54e8018e0 to your computer and use it in GitHub Desktop.
An example of repairing a serialized PHP string. See: https://stackoverflow.com/a/21389439/1170664
<?php
// @see https://stackoverflow.com/a/21389439/1170664
namespace Corcel\Model\Meta;
PostMeta::where('meta_key', '_wp_attachment_metadata')->chunk(100, function ($collection) {
foreach ($collection as $meta) {
// recalculate the length of the elements in the serialized array
$fixed_data = preg_replace_callback('!s:(\d+):"(.*?)";!', function($match) {
return ($match[1] == strlen($match[2])) ? $match[0] : 's:' . strlen($match[2]) . ':"' . $match[2] . '";';
}, $meta->meta_value);
$meta->meta_value = $fixed_data;
$meta->save();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment