从 php.ini 中的速记字节表示法获取字节值

发布于 2024-11-26 09:24:12 字数 298 浏览 2 评论 0 原文

当使用 ini_get('upload_max_filesize') 和 ini_get('post_max_size') 等函数返回的字符串中获取字节值="http://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes" rel="noreferrer">简写字节符号?例如从 4M 获取 4194304 ?我可以将一个函数组合在一起来执行此操作,但如果没有某种内置的方法来执行此操作,我会感到惊讶。

Is there any way to get the byte values from the strings returned from functions like ini_get('upload_max_filesize') and ini_get('post_max_size') when they are using shorthand byte notation? For example get 4194304 from 4M ? I could hack together a function that does this but I would be surprised if there wasn't some built in way of doing this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

遗弃M 2024-12-03 09:24:12

您链接到的段落结束:

您不能在 php.ini 之外使用这些速记符号,而是
使用字节的整数值。 请参阅ini_get() 文档了解
有关如何转换这些值的示例。

这将引导您看到类似的内容(我稍作修改):

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

使用它 像这样

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

没有内置函数来执行此任务;回想一下,实际上,INI 设置是为 PHP 内部使用而设计的。 PHP 源码使用了与上面类似的函数。

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.

烟柳画桥 2024-12-03 09:24:12

嘎!刚刚在 http://www.php.net/manual 上找到了答案/en/function.ini-get.php

只需要 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;
}

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;
}
菩提树下叶撕阳。 2024-12-03 09:24:12

这是我的建议,应该是面向未来的(关于 PHP 版本 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;
}

...玩得开心!

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!

愛上了 2024-12-03 09:24:12

我找到了很多解决方案来解决这个问题,应该有一个内置函数来解决这个问题。无论如何,关于这个问题,有两件事需要强调:

  1. 这是 PHP 的速记字节值所特有的,如下所述: http://php.net/manual/en/faq.using.php#faq.using.shorthandbytes 并且只能在这种情况下使用。
  2. 大多数现代 IDE 都会对没有 breakswitch 语句发出警告。

这是一个涵盖所有内容的解决方案:

/**
 * Convert PHP's directive values to bytes (including the shorthand syntax).
 *
 * @param string $directive The PHP directive from which to get the value from.
 *
 * @return false|int Returns the value of the directive in bytes if available, otherwise false.
 */
function php_directive_value_to_bytes($directive) {
    $value = ini_get($directive);

    // Value must be a string.
    if (!is_string($value)) {
        return false;
    }

    preg_match('/^(?<value>\d+)(?<option>[K|M|G]*)$/i', $value, $matches);

    $value = (int) $matches['value'];
    $option = strtoupper($matches['option']);

    if ($option) {
        if ($option === 'K') {
            $value *= 1024;
        } elseif ($option === 'M') {
            $value *= 1024 * 1024;
        } elseif ($option === 'G') {
            $value *= 1024 * 1024 * 1024;
        }
    }

    return $value;
}

I found a lot of solution to solve this problem and there should have been a builtin function to solve this. In any case two things to highlight about this problem:

  1. This is very specific to PHP's shorthand byte values as explained here: http://php.net/manual/en/faq.using.php#faq.using.shorthandbytes and should only be used in this context.
  2. Most modern IDEs will give warning for switch statements without break.

Here is a solution that covers everything:

/**
 * Convert PHP's directive values to bytes (including the shorthand syntax).
 *
 * @param string $directive The PHP directive from which to get the value from.
 *
 * @return false|int Returns the value of the directive in bytes if available, otherwise false.
 */
function php_directive_value_to_bytes($directive) {
    $value = ini_get($directive);

    // Value must be a string.
    if (!is_string($value)) {
        return false;
    }

    preg_match('/^(?<value>\d+)(?<option>[K|M|G]*)$/i', $value, $matches);

    $value = (int) $matches['value'];
    $option = strtoupper($matches['option']);

    if ($option) {
        if ($option === 'K') {
            $value *= 1024;
        } elseif ($option === 'M') {
            $value *= 1024 * 1024;
        } elseif ($option === 'G') {
            $value *= 1024 * 1024 * 1024;
        }
    }

    return $value;
}
日暮斜阳 2024-12-03 09:24:12

正如我发现的那样,上面显示的版本都不再适用于 PHP 7.2x。通过使用此版本,对于 PHP 7.0 + 7.1,它可以工作,但不能用于 PHP 7.x
厄尼

private function return_bytes ($val) {
        if(empty($val))return 0;
        $val = trim($val);
        preg_match('#([0-9]+)[\s]*([a-z]+)#i', $val, $matches);
        $last = '';
        if(isset($matches[2])){
            $last = $matches[2];
        }
        if(isset($matches[1])){
            $val = (int) $matches[1];
        }
        switch (strtolower($last)){
            case 'g':
            case 'gb':  
            $val *= 1024;
            case 'm':
            case 'mb':
            $val *= 1024;
            case 'k':
            case 'kb':
            $val *= 1024;
        }
        return (int) $val;
    }

Neither one of the Version shown above will work with PHP 7.2x anymore, as I found out.By use of this,with PHP 7.0 + 7.1, it works, but not with PHP 7.x
Ernie

private function return_bytes ($val) {
        if(empty($val))return 0;
        $val = trim($val);
        preg_match('#([0-9]+)[\s]*([a-z]+)#i', $val, $matches);
        $last = '';
        if(isset($matches[2])){
            $last = $matches[2];
        }
        if(isset($matches[1])){
            $val = (int) $matches[1];
        }
        switch (strtolower($last)){
            case 'g':
            case 'gb':  
            $val *= 1024;
            case 'm':
            case 'mb':
            $val *= 1024;
            case 'k':
            case 'kb':
            $val *= 1024;
        }
        return (int) $val;
    }
柏林苍穹下 2024-12-03 09:24:12

这是一种方法。

$str=ini_get('upload_max_filesize');
preg_match('/[0-9]+/', $str, $match);

echo ($match[0] * 1024 * 1024); // <--This is MB converted to bytes

Here is one way.

$str=ini_get('upload_max_filesize');
preg_match('/[0-9]+/', $str, $match);

echo ($match[0] * 1024 * 1024); // <--This is MB converted to bytes
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文