CSS & Regex, switching values of padding or margin property

僤鯓⒐⒋嵵緔 提交于 2019-12-25 00:37:27

问题


I have CSS code where I want to find Padding -or- Margin Properties (where it has 4 values) and to switch the left value with right value:
e.g. padding: 1px 2px 3px 4px;
- to be like this -
padding: 1px 4px 3px 2px;
How can I achieve this with regex & PHP? :-)


回答1:


Try this:

preg_replace(
    '/padding:\s*?(\d*px)\s*(\d*px)\s*(\d*px)\s*(\d*px)\s*?;/i',
    'padding: $1 $4 $3 $2;',
    $css
);

Explanation

\s matches a space

\d matches a digit

\d*px will match any digits followed by a 'px'



来源:https://stackoverflow.com/questions/13191473/css-regex-switching-values-of-padding-or-margin-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!