【问题标题】:PHP output so long [duplicate]PHP输出这么长[重复]
【发布时间】:2014-06-21 09:42:27
【问题描述】:

嘿,我制作了一个显示远程 url 大小的程序,但我不希望输出像 4.3224256231 MB 那样,我希望只有前 3 位数字应该像 4.32 MB 这样的输出。这是我的 PHP 代码:-

<?php

function remote_file_size($url){
    $head = "";
    $url_p = parse_url($url);

    $host = $url_p["host"];
    if(!preg_match("/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/",$host)){

        $ip=gethostbyname($host);
        if(!preg_match("/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/",$ip)){

            return -1;
        }
    }
    if(isset($url_p["port"]))
    $port = intval($url_p["port"]);
    else
    $port    =    80;

    if(!$port) $port=80;
    $path = $url_p["path"];

    $fp = fsockopen($host, $port, $errno, $errstr, 20);
    if(!$fp) {
        return false;
        } else {
        fputs($fp, "HEAD "  . $url  . " HTTP/1.1\r\n");
        fputs($fp, "HOST: " . $host . "\r\n");
        fputs($fp, "User-Agent: http://www.example.com/my_application\r\n");
        fputs($fp, "Connection: close\r\n\r\n");
        $headers = "";
        while (!feof($fp)) {
            $headers .= fgets ($fp, 128);
            }
        }
    fclose ($fp);

    $return = -2;
    $arr_headers = explode("\n", $headers);
    foreach($arr_headers as $header) {

        $s1 = "HTTP/1.1";
        $s2 = "Content-Length: ";
        $s3 = "Location: ";

        if(substr(strtolower ($header), 0, strlen($s1)) == strtolower($s1)) $status = substr($header, strlen($s1));
        if(substr(strtolower ($header), 0, strlen($s2)) == strtolower($s2)) $size   = substr($header, strlen($s2));
        if(substr(strtolower ($header), 0, strlen($s3)) == strtolower($s3)) $newurl = substr($header, strlen($s3));  
    }

    if(intval($size) > 0) {
        $return=intval($size);
    } else {
        $return=$status;
    }

    if (intval($status)==302 && strlen($newurl) > 0) {

        $return = remote_file_size($newurl);
    }
    return $return;
}
$file= remote_file_size("http://funchio.com/mp3/download.php?hash=7RLenrUE&name=nagada%20sang%20dhol%20baje-ramleela");
$p=$file/1048;
$m=$p/1048;
echo $m. " MB";
substr_replace($m, "", -9)


?>

请帮我做这件事。

【问题讨论】:

  • 这真的是你应该能够自己找到的东西......只需在谷歌中输入“PHP 整数”即可产生 大量 的结果和示例。在 round/floor/ceil/sprintf 上的 PHP 文档中也有很多示例。
  • 使用 PHP Round 函数
  • 另外:我不认为除以1048 会产生正确的结果... 千字节中有2^10 = 1024 字节,千字节 => 兆字节等也是如此.
  • 用户round()函数

标签: php substr


【解决方案1】:

用户number_format(); 第一个参数是被格式化的数字。第二个参数是小数位数。

echo number_format($m,3). " MB";

【讨论】:

    【解决方案2】:

    试试..

    echo round($m, 2);
    
    echo round(4.3224256231, 2); 
    

    输出: 4.32

    您还可以添加指定 GB 的附加逻辑

    【讨论】:

      【解决方案3】:

      使用这个简单的函数来获取远程文件大小。你的功能太多余了。

      function remote_filesize($url) {
          static $regex = '/^Content-Length: *+\K\d++$/im';
          if (!$fp = @fopen($url, 'rb')) {
              return false;
          }
          if (
              isset($http_response_header) &&
              preg_match($regex, implode("\n", $http_response_header), $matches)
          ) {
              return (int)$matches[0];
          }
          return strlen(stream_get_contents($fp));
      }
      

      【讨论】:

        猜你喜欢
        • 2013-03-04
        • 1970-01-01
        • 1970-01-01
        • 2010-09-28
        • 1970-01-01
        • 1970-01-01
        • 2015-12-18
        • 2011-06-18
        • 2019-02-28
        相关资源
        最近更新 更多