<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 等转换为字节

        PHP convert KB MB GB TB etc to Bytes(PHP 将 KB MB GB TB 等转换为字节)

              <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 等转换为字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Converting string to MySQL timestamp format in php(在php中将字符串转换为MySQL时间戳格式)
                  datetime to timestamp(日期时间到时间戳)
                  PHP timestamp date to user timezone(PHP时间戳日期到用户时区)
                  Converting TIMESTAMP to unix time in PHP?(在 PHP 中将 TIMESTAMP 转换为 unix 时间?)
                  Convert ISO 8601 to unixtimestamp(将 ISO 8601 转换为 unixtimestamp)
                  Finding days between 2 unix timestamps in php(在php中查找2个unix时间戳之间的天数)
                • <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>

                        <tbody id='PVE7O'></tbody>

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

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