<i id='FKswK'><tr id='FKswK'><dt id='FKswK'><q id='FKswK'><span id='FKswK'><b id='FKswK'><form id='FKswK'><ins id='FKswK'></ins><ul id='FKswK'></ul><sub id='FKswK'></sub></form><legend id='FKswK'></legend><bdo id='FKswK'><pre id='FKswK'><center id='FKswK'></center></pre></bdo></b><th id='FKswK'></th></span></q></dt></tr></i><div id='FKswK'><tfoot id='FKswK'></tfoot><dl id='FKswK'><fieldset id='FKswK'></fieldset></dl></div>
  • <small id='FKswK'></small><noframes id='FKswK'>

    <legend id='FKswK'><style id='FKswK'><dir id='FKswK'><q id='FKswK'></q></dir></style></legend>
      <bdo id='FKswK'></bdo><ul id='FKswK'></ul>
    <tfoot id='FKswK'></tfoot>
      1. PHP 将 KB MB GB TB 等转换为字节

        时间:2024-04-13

              <small id='KqNl1'></small><noframes id='KqNl1'>

                <tbody id='KqNl1'></tbody>
              <i id='KqNl1'><tr id='KqNl1'><dt id='KqNl1'><q id='KqNl1'><span id='KqNl1'><b id='KqNl1'><form id='KqNl1'><ins id='KqNl1'></ins><ul id='KqNl1'></ul><sub id='KqNl1'></sub></form><legend id='KqNl1'></legend><bdo id='KqNl1'><pre id='KqNl1'><center id='KqNl1'></center></pre></bdo></b><th id='KqNl1'></th></span></q></dt></tr></i><div id='KqNl1'><tfoot id='KqNl1'></tfoot><dl id='KqNl1'><fieldset id='KqNl1'></fieldset></dl></div>
              <legend id='KqNl1'><style id='KqNl1'><dir id='KqNl1'><q id='KqNl1'></q></dir></style></legend>
              <tfoot id='KqNl1'></tfoot>

                • <bdo id='KqNl1'></bdo><ul id='KqNl1'></ul>
                  本文介绍了PHP 将 KB MB GB TB 等转换为字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述


                  我在问如何转换 KB MB GB TB &公司成字节.
                  例如:


                  I'm asking how to convert KB MB GB TB & co. into bytes.
                  For example:

                  byteconvert("10KB") // => 10240
                  byteconvert("10.5KB") // => 10752
                  byteconvert("1GB") // => 1073741824
                  byteconvert("1TB") // => 1099511627776
                  

                  等等……

                  哇.4年前我问过这个问题.这种事情真的可以告诉你随着时间的推移你进步了多少!

                  wow. I've asked this question over 4 years ago. Thise kind of things really show you how much you've improved over time!

                  推荐答案

                  这里有一个函数可以实现:

                  Here's a function to achieve this:

                  function convertToBytes(string $from): ?int {
                      $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
                      $number = substr($from, 0, -2);
                      $suffix = strtoupper(substr($from,-2));
                  
                      //B or no suffix
                      if(is_numeric(substr($suffix, 0, 1))) {
                          return preg_replace('/[^d]/', '', $from);
                      }
                  
                      $exponent = array_flip($units)[$suffix] ?? null;
                      if($exponent === null) {
                          return null;
                      }
                  
                      return $number * (1024 ** $exponent);
                  }
                  
                  $testCases = ["13", "13B", "13KB", "10.5KB", "123Mi"];
                  var_dump(array_map('convertToBytes', $testCases));
                  

                  输出:

                  array(5) { [0]=> int(13) [1]=> int(13) [2]=> int(13312) [3]=>int(10752) [4]=> NULL } int(1)

                  array(5) { [0]=> int(13) [1]=> int(13) [2]=> int(13312) [3]=> int(10752) [4]=> NULL } int(1)

                  这篇关于PHP 将 KB MB GB TB 等转换为字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 SQL 中使用 PHP 为 DATETIME 转换日期 下一篇:在 PHP 中使用 settype 而不是使用括号进行类型转换,有什么区别?

                  相关文章

                • <legend id='PVE7O'><style id='PVE7O'><dir id='PVE7O'><q id='PVE7O'></q></dir></style></legend>

                • <i id='PVE7O'><tr id='PVE7O'><dt id='PVE7O'><q id='PVE7O'><span id='PVE7O'><b id='PVE7O'><form id='PVE7O'><ins id='PVE7O'></ins><ul id='PVE7O'></ul><sub id='PVE7O'></sub></form><legend id='PVE7O'></legend><bdo id='PVE7O'><pre id='PVE7O'><center id='PVE7O'></center></pre></bdo></b><th id='PVE7O'></th></span></q></dt></tr></i><div id='PVE7O'><tfoot id='PVE7O'></tfoot><dl id='PVE7O'><fieldset id='PVE7O'></fieldset></dl></div>

                    • <bdo id='PVE7O'></bdo><ul id='PVE7O'></ul>
                  1. <tfoot id='PVE7O'></tfoot>

                      <small id='PVE7O'></small><noframes id='PVE7O'>