public static function sizeToBytes($str)
    {
        if (!preg_match('/^(\d+(\.\d+)?)([bkmg]*)$/i', trim($str), $parts)) {
            throw new \Exception('Failed to convert string to bytes, incorrect size given: ' . $str);
        }

        switch (substr(strtolower($parts[3]), 0, 1)) {
            case 'g':
                $size = (int)($parts[1] * 1073741824);
                break;
            case 'm':
                $size = (int)($parts[1] * 1048576);
                break;
            case 'k':
                $size = (int)($parts[1] * 1024);
                break;
            case 'b':
            default:
                $size = (int)$parts[1];
                break;
        }

        return $size;
    }
public static function sizeToBytes($str)
    {
        if (!preg_match('/^(\d+(\.\d+)?)([bkmg]*)$/i', trim($str), $parts)) {
            throw new \Exception('Failed to convert string to bytes, incorrect size given: ' . $str);
        }

        switch (substr(strtolower($parts[3]), 0, 1)) {
            case 'g':
                $size = (int)($parts[1] * 1073741824);
                break;
            case 'm':
                $size = (int)($parts[1] * 1048576);
                break;
            case 'k':
                $size = (int)($parts[1] * 1024);
                break;
            case 'b':
            default:
                $size = (int)$parts[1];
                break;
        }

        return $size;
    }
public static function sizeToBytes ( $ str ) { if ( ! preg_match ( '/^(\d+(\.\d+)?)([bkmg]*)$/i' , trim ( $ str ) , $ parts ) ) { throw new \ Exception ( 'Failed to convert string to bytes, incorrect size given: ' . $ str ) ; } switch ( substr ( strtolower ( $ parts [ 3 ] ) , 0 , 1 ) ) { case 'g' : $ size = ( int ) ( $ parts [ 1 ] * 1073741824 ) ; break ; case 'm' : $ size = ( int ) ( $ parts [ 1 ] * 1048576 ) ; break ; case 'k' : $ size = ( int ) ( $ parts [ 1 ] * 1024 ) ; break ; case 'b' : default : $ size = ( int ) $ parts [ 1 ] ; break ; } return $ size ; }
Make a size like 2M to bytes

Some examples:
'1024b' => '1024',
'1B' => '1',
'1kb' => '1024',
'21k' => '21504',
'5m' => '5242880',
'5M' => '5242880',
'1G' => '1073741824',
'4g' => '4294967296',
'1.4mb' => '1468006'

@param string $str
@return int
@throws \Exception
Make a size like 2M to bytes