Dimension check

We need to check the dimension (%,etc.) to compare with zero and find out if a number was entered.
Nothing smarter than heaps of intval and strpos.

There is another option:

preg_match('/^\d+(%|px)$/',$num,$match);

But it seems, though more beautiful, but more costly. Saving on matches?

Question:
How to beautifully and quickly separate the correct input of a number with a dimension from any other garbage and bring it to a previously known dimension in case of a correct input?

Um. Pulls on a learning task. confused


Answer 1, authority 100%

Save on matches, also don’t forget that the number of percentages can be a float, for example 33.33%, reworking REGEXP is not much more difficult, but you will have to suffer with the first example…

Regexp for float values:

preg_match('/^([\d.]+)(%|px)$/',$num,$match);

then you get $match[1] – value, $match[2] – dimension.