Last active
August 12, 2019 19:16
-
-
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
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 | |
// @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