get byte value from shorthand byte notation in php.ini

The paragraph you linked to ends:

You may not use these shorthand notations outside of php.ini, instead use an integer value of bytes. See the ini_get() documentation for an example on how to convert these values.

This leads you to something like this (which I have slightly modified):

function return_bytes($val)
{
    $val  = trim($val);

    if (is_numeric($val))
        return $val;

    $last = strtolower($val[strlen($val)-1]);
    $val  = substr($val, 0, -1); // necessary since PHP 7.1; otherwise optional

    switch($last) {
        // The 'G' modifier is available since PHP 5.1.0
        case 'g':
            $val *= 1024;
        case 'm':
            $val *= 1024;
        case 'k':
            $val *= 1024;
    }

    return $val;
}

Use it like so:

echo return_bytes("3M");
// Output: 3145728

There is no built-in function to perform this task; recall that, really, INI settings are designed for use internally within PHP. The PHP source uses a similar function to the above.


This is my suggestion which should be future-proof (with regard to PHP Versions 7++)!

function return_bytes($val, $gotISO = false) {        
    // This is my ultimate "return_bytes" function!
    // Converts (not only) the PHP shorthand notation which is returned by e.g. "ini_get()"
    // Special features:
    // Doesn't need regular expression and switch-case conditionals.
    // Identifies beside "Kilo", "Mega" and "Giga" also "Tera", "Peta", "Exa", "Zetta", and "Yotta" too!
    // Ignores spaces and doesn't make no difference between e.g. "M" or "MB"!
    // By default possible commas (,) as thousand separator will be erased. Example: "1,000.00" will "be 1000.00".
    // If ($gotISO == true) it converts ISO formatted values like "1.000,00" into "1000.00".
    $pwr = 0;
    if(empty($val)) return 0;
    $val  = trim($val);
    if (is_numeric($val)) return $val;
    if ($gotISO) {
        $val = str_replace('.','',$val); // wipe possibe thousend separators (.)
        $val = str_replace(',','.',$val); // convert ISO comma to value point
    } else {
        $val = str_replace(',','',$val); // wipe possibe thousend separators (,)
    }
    $val = str_replace(' ','',$val);
    if (floatval($val) == 0) return 0;
    if (stripos($val, 'k') !== false) $pwr = 1;
        elseif (stripos($val, 'm') !== false) $pwr = 2;
        elseif (stripos($val, 'g') !== false) $pwr = 3;
        elseif (stripos($val, 't') !== false) $pwr = 4;
        elseif (stripos($val, 'p') !== false) $pwr = 5;
        elseif (stripos($val, 'e') !== false) $pwr = 6;
        elseif (stripos($val, 'z') !== false) $pwr = 7;
        elseif (stripos($val, 'y') !== false) $pwr = 8;
    $val *= pow(1024, $pwr);
    return $val;
}

... have fun with it!


Gah! Just found the answer on http://www.php.net/manual/en/function.ini-get.php

Just needed to RTM...

function return_bytes($val) {
    $val = trim($val);
    $last = strtolower($val[strlen($val)-1]);
    switch($last) {
        // The 'G' modifier is available since PHP 5.1.0
        case 'g':
            $val *= 1024;
        case 'm':
            $val *= 1024;
        case 'k':
            $val *= 1024;
    }

    return $val;
}

Tags:

Php

Byte