单个preg_replace
调用可以使用数组作为模式和替换来做到这一点:
$data_style = "position:absolute; top:0px; left:700px; width:500px; height:400px;
vertical-align: top; padding-top: 0px; padding-right: 0px; padding-bottom: 0px;
padding-left: 0px; display: list-item;";
$patterns = array('/(top\s*:\s*)\d+(px)/', '/(left\s*:\s*)\d+(px)/',
'/(width\s*:\s*)\d+(px)/', '/(height\s*:\s*)\d+(px)/');
$replacements = array('${1}10${2}', '${1}500${2}', '${1}300${2}', '${1}450${2}');
$data_style = preg_replace($patterns, $replacements, $data_style);
echo $data_style;
输出:
position:absolute; top:10px; left:500px; width:300px; height:450px;
vertical-align: top; padding-top: 10px; padding-right: 0px; padding-bottom: 0px;
padding-left: 500px; display: list-item;