我想出了这个。它有点冗长,但是,它完成了工作..
function rtlPadding($string)
{
if (preg_match_all('~([0-9]+(px|%)?)~i', $string, $matches))
{
$c = count($matches['1']);
$p = $matches['1'];
if ($c == 4)
$order = implode(' ', array($p['0'], $p['3'], $p['2'], $p['1']));
else if ($c == 3)
$order = implode(' ', array($p['0'], 0, $p['2'], $p['1']));
else
$order = implode(' ', $p);
return 'padding: ' . $order . ';';
}
return false;
}
我测试了它,它似乎工作得很好,至少你提供的数据是这样。
$paddings = array(
'padding: 1px 2px;' => 'padding: 1px 2px;',
'padding: 0 2px 4px;' => 'padding: 0 0 4px 2px;',
'padding: 1px 0 3px 4px;' => 'padding: 1px 4px 3px 0;',
'padding: 1px 2px 0 4px;' => 'padding: 1px 4px 0 2px;',
'padding: 1px 2px 3px 0;' => 'padding: 1px 0 3px 2px;'
);
foreach ($paddings as $given => $expected)
{
$return = rtlPadding($given);
if ($return === $expected)
echo 'Everything OK!' . PHP_EOL;
else
echo 'Error! Expected ' . $expected . ' Got ' . $return . PHP_EOL;
}
// Output: Everything Ok, 5 times :D