donglv6960
2013-05-29 20:58
浏览 63
已采纳

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

图片转代码服务由CSDN问答提供 功能建议

RTLIT ruby​​工具用于在css文件中从LTR切换到RTL,但它也不会更改填充 ,保证金留下来例如:

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; 
   
 
 

需要 将此值更改为:

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

我们需要始终将第二个值切换为第4个值,如果它们只是3,则将第二个值切换为第四个值 将第二个设置为0

如果只有2个值,请保持原样。

我需要在ruby或php中使用该正则表达式

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dqh1992 2013-05-29 21:14
    已采纳

    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')
    
    点赞 评论
  • douya6229 2013-05-29 22:26

    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
    
    点赞 评论

相关推荐 更多相似问题