You can use a regex like this:
(d+(?:px|em|%)?)s+(d+(?:px|em|%)?)s+(d+(?:px|em|%)?)s+(d+(?:px|em|%)?)
And implement it like so:
$subject = '10px 20% 40px 0';
$result = preg_replace('/(d+(?:px|em|%)?)s+(d+(?:px|em|%)?)s+(d+(?:px|em|%)?)s+(d+(?:px|em|%)?)/i', '$1 $4 $3 $2', $subject);
echo $result; // Prints: 10px 0 40px 20%
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…