RTLIT css padding rtl正则表达式

RTLIT ruby tool is used to switch from LTR to RTL in css files, but it doesn't also change the padding,margin left to righ for example :

https://github.com/zohararad/rtlit/blob/master/lib/rtlit/converter.rb

padding: 1px 2px;
padding: 0 2px 4px;
padding: 1px 0 3px 4px;
padding: 1px 2px 0 4px;
padding: 1px 2px 3px 0;

need to change this values to be :

padding: 1px 2px;
padding: 0 0 4px 2px;
padding: 1px 4px 3px 0;
padding: 1px 4px 0 2px;
padding: 1px 0 3px 2px;

we need to switch always the second value with the 4th value and if they are only 3 switch the second value to the forth and set the second to 0

also if there are only 2 values , leave it as it is.

I need that regular expression in ruby or php

try this in php:

$result = preg_replace ('~\b(?:padding|margin)\b\h*:\h*\K(-?\d+[a-z%]*)\h+(-?\d+[a-z%]*)\h+(-?\d+[a-z%]*)\h+(-?\d+[a-z%]*)\h*(?=;)~i', '$1 $4 $3 $2', $string);

with ruby:

my_result = my_str.sub( %r{(?i)\b(padding|margin)\b *: *(-?\d+[a-z%]*) +(-?\d+[a-z%]*) +(-?\d+[a-z%]*) +(-?\d+[a-z%]*) *(?=;)}, '$1: $2 $5 $4 $3')

I came up with this. Its a little verbose, but, it does the job..

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

I tested it and It seems to work just fine, at least with the data you gave.

$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