You should probably not run preg_replace
on the meta data for a _wp_attachment_metadata
field as the data is serialized. I would recommend instead you write a small script to loop through the images, unserialize the data, update the value, serialize and store the modified array. Here’s a sample function which does just that:
function wpse_135525_fix_imagepaths() {
$images = get_posts(array(
'post_type' => 'attachment',
'post_mime_types' => 'image',
'posts_per_page' => -1,
// Extra filtering, e.g. by post_date etc.
));
foreach ($images as $image) {
$attachment_meta = get_post_meta($image->ID, '_wp_attachment_metadata', true);
if ($attachment_meta) {
$attachment_meta = unserialize($attachment_meta);
$filename = $attachment_meta['file'];
$filename = preg_replace('/(\d)\1/', '$1', $filename); // Replace any two repeating digits (44 => 4, 11 => 1 etc.)
$attachment_meta['file'] = $filename;
update_post_meta($image->ID, '_wp_attachment_metadata', serialize($attachment_meta));
}
}
}
You’ll probably want to limit the post query as to only process attachments created within a defined period of time, and the regex should need some improvement to prevent it from replacing any repeating digits in the first part of filenames etc.