Checks if a given string is a CSS valid number. If it is, an array containing the value and unit is returned

Parameters

string $string:

Return value

array ('unit' if unit is found or '' if no unit exists, number value) or false if no number

1 call to csstidy_optimise::AnalyseCssNumber()
csstidy_optimise::compress_numbers in advagg_css_compress/csstidy/class.csstidy_optimise.inc
Compresses numbers (ie. 1.0 becomes 1 or 1.100 becomes 1.1 )

File

advagg_css_compress/csstidy/class.csstidy_optimise.inc, line 411

Class

csstidy_optimise
CSS Optimising Class

Code

function AnalyseCssNumber($string) {
    // most simple checks first
    if (strlen($string) == 0 || ctype_alpha($string[0])) {
        return false;
    }
    $units =& $GLOBALS['csstidy']['units'];
    $return = array(
        0,
        '',
    );
    $return[0] = floatval($string);
    if (abs($return[0]) > 0 && abs($return[0]) < 1) {
        if ($return[0] < 0) {
            $return[0] = '-' . ltrim(substr($return[0], 1), '0');
        }
        else {
            $return[0] = ltrim($return[0], '0');
        }
    }
    // Look for unit and split from value if exists
    foreach ($units as $unit) {
        $expectUnitAt = strlen($string) - strlen($unit);
        if (!($unitInString = stristr($string, $unit))) {
            // mb_strpos() fails with "false"
            continue;
        }
        $actualPosition = strpos($string, $unitInString);
        if ($expectUnitAt === $actualPosition) {
            $return[1] = $unit;
            $string = substr($string, 0, -strlen($unit));
            break;
        }
    }
    if (!is_numeric($string)) {
        return false;
    }
    return $return;
}