Is there an easy way in PHP to convert from strings like '256M', '180K', '4G' to their integer equivalents?

前端 未结 6 565
抹茶落季
抹茶落季 2021-01-08 01:22

I need to test the value returned by ini_get(\'memory_limit\') and increase the memory limit if it is below a certain threshold, however this ini_get(\'me

6条回答
  •  粉色の甜心
    2021-01-08 01:52

    From the PHP website for ini_get():

    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;
    }
    

提交回复
热议问题