Regular Expression to get the size value of a CSS property?

Posted by thiesdiggity on Stack Overflow See other posts from Stack Overflow or by thiesdiggity
Published on 2012-06-08T04:09:04Z Indexed on 2012/06/08 4:40 UTC
Read the original article Hit count: 176

Filed under:
|
|
|

I wrote a regular expression to get the size from a CSS property:

/(([+-]?\d*\.?\d+(\s)*(px|em|ex|pt|in|pc|mm|cm)?)|thin|medium|thick)(\s|;|$)/i

but for some reason its not working as intended. For example, when I run the following:

preg_match_all('/(([+-]?\d*\.?\d+(\s)*(px|em|ex|pt|in|pc|mm|cm)?)|thin|medium|thick)(\s|;|$)/i', 
"border-bottom:1px solid #99999;", $matches);

It outputs:

1px
99999;

But I only want the 1px value returned.

I understand why its returning the above but can't seem to figure out how to only return the size and not the color value. I tried using the following negative lookbehind but it's not working either:

/(((?<!#\d{3}|#\d{6})[+-]?\d*\.?\d+(\s)*(px|em|ex|pt|in|pc|mm|cm)?)|thin|medium|thick)(\s|;|$)/i

I want to use preg_match_all for those CSS properties that can have multiple size values (i.e. margin).

Anyone have any ideas how to get this regex to return only the size values?

Thanks for your help!

© Stack Overflow or respective owner

Related posts about php

Related posts about css