如何从已序列化的数组中删除特定子阵列?

时间:2012-02-24 22:27:50

标签: php arrays regex wordpress serialization

我正在使用新的Wordpress插件,但我需要一个正则表达式的帮助。

我有这个

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:"";}}

但我想删除缩略图子阵列:

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";}

最终产生这个:

'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:"";}}

伪代码:

chain { chain "name image " chain}

我有这个想法:

$var = preg_replace("(.+)\{(.+)(sopa_nicearma\-15x10\.jpg)(.+)\}/", "", $var);

但这不能按预期工作,我明白了:

}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:"";}}

2 个答案:

答案 0 :(得分:6)

您不应该使用正则表达式执行此操作。这些是序列化对象,如果您手动编辑其中的任何内容,这会更改任何包含字符串的长度,您还必须更改长度参数。

相反,您应该通过$data = unserialize($data);进行更改来重新序列化数据,然后使用$data = serialize($data);再次对其进行序列化。

答案 1 :(得分:0)

由于单引号上加了斜杠,因此看来您发布的序列化字符串在技术上是无效的。可能是由于复制var_export()的外部单引号或其他内容之间的值而导致发布不准确。不管是什么原因,我都会从输入中去除斜线(可以使用许多不同的功能来完成),以便可以对数据进行反序列化。

unserialize()将数据转换为数组后,可以使用unset()删除不需要的缩略图子数组。

完成修改后,您可以简单地重新序列化数据,而不会造成数据损坏的风险。

代码:(Demo

$string = <<<STRING
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:"";}}
STRING;

$array = unserialize(stripslashes($string));  // remove unnecessary slashes to make valid

unset($array['sizes']['thumbnail']);  // remove the targeted subarray

//print_r($array);  // uncomment if you want to see the updated array
echo "Old: $string\n";
echo "New: " , serialize($array);

输出:

Old: 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:"";}}
// the differences ->                                                                                                                     recalculated subarray size value -^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- this is removed
New: 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:2:{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:"";}}