I’m working in new WordPress plugin, but I need help with one regular expression.
I have this
a:6:{s:5:"width";s:4:"3000";s:6:"height";s:4:"2100";s:14:"hwstring_small";s:23:"height='89' width='128'";s:4:"file";s:25:"2012/02/sopa_nicearma.jpg";s:5:"sizes";a:3:{s:9:"thumbnail";a:3:{s:4:"file";s:23:"sopa_nicearma-15x10.jpg";s:5:"width";s:2:"15";s:6:"height";s:2:"10";}s:6:"medium";a:3:{s:4:"file";s:25:"sopa_nicearma-300x210.jpg";s:5:"width";s:3:"300";s:6:"height";s:3:"210";}s:5:"large";a:3:{s:4:"file";s:25:"sopa_nicearma-700x490.jpg";s:5:"width";s:3:"700";s:6:"height";s:3:"490";}
}s:10:"image_meta";a:10:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";}}
but I want to delete the thumbnail subarray:
s:9:"thumbnail";a:3:{s:4:"file";s:23:"sopa_nicearma-15x10.jpg";s:5:"width";s:2:"15";s:6:"height";s:2:"10";}
to produce this in the end:
'a:6:{s:5:"width";s:4:"3000";s:6:"height";s:4:"2100";s:14:"hwstring_small";s:23:"height='89' width='128'";s:4:"file";s:25:"2012/02/sopa_nicearma.jpg";s:5:"sizes";a:3:{s:6:"medium";a:3:{s:4:"file";s:25:"sopa_nicearma-300x210.jpg";s:5:"width";s:3:"300";s:6:"height";s:3:"210";}s:5:"large";a:3:{s:4:"file";s:25:"sopa_nicearma-700x490.jpg";s:5:"width";s:3:"700";s:6:"height";s:3:"490";}
}s:10:"image_meta";a:10:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";}}
pseudo code:
chain { chain "name image " chain}
I have this idea:
$var = preg_replace("(.+){(.+)(sopa_nicearma-15x10.jpg)(.+)}/", "", $var);
but this doesn’t work as desired, I get this:
}s:10:"image_meta";a:10:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";}}
You should not do this with regular expressions. These are serialized objects and if you edit anything in them manually which changes length of any contained string, you have to change the length parameter as well.
Instead you should unserialise data by doing
$data = unserialize($data);
make changes and then serialise it again with$data = serialize($data);
.It appears that your posted serialized string is technically invalid due to the addition of slashes on your single quotes. Maybe this is a posting inaccuracy cause by copying the value between the outer single quotes of
var_export()
or something else. Regardless of the cause, I’ll strip the slashes from the input (this could be done with a number of different functions) so that the data can be unserialized.After
unserialize()
converts the data into an array, you can useunset()
to remove the unwanted thumbnail subarray.With the modifications complete, you can simply re-serialize the data with no risk of data corruption.
Code: (Demo)
Output: