1

PHP: Wie kann ich Bytes in ein lesbares Format (KB, MB, GB, …) konvertieren?

Ich brauchte die Anzahl der Bytes in einem lesbaren Format mit 2 Stellen nach dem Komma und habe folgende logarithmische Funktion verwendet. Hier wird der Binärpräfix (Basis 2) und nicht der Dezimalpräfix (Basis 10) verwendet, obwohl das nicht ganz korrekt ist. Wer es korrekt haben möchte ändert einfach den Parameter zu $base=1000. Für mich ist ein Kilobyte immer noch 1024 Bytes. Festplattenhersteller sehen das manchmal unterschiedlich, wo ein Terrabyte (TB) mal 1.000.000.000.000 Bytes und mal (TiB) 1.099.511.627.776 Bytes sind.


Dezimalpräfixe:                                  Binärpräfixe:

Kilobyte (kB) 1000 = 103                    Kibibyte (KiB) 1024 = 210


PHP Funktion:

// convert bytes format
function format_Bytes($size,$level=0,$precision=2,$base=1024) 
{
    if ($size === '0' || $size === null) {
            return "0 B";
        }
  else
    $unit = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB','YB');
    $times = floor(log($size,$base));
    return sprintf("%.".$precision."f",$size/pow($base,($times+$level)))." ".$unit[$times+$level];
}

Links:

Github Gist: https://gist.github.com/ElectroDrome/374b42422452561faacbde047300ff45

Wikipedia: https://de.wikipedia.org/wiki/Byte




PHP: Funktion: Aus einer TXT-Datei eine HTML-Tabelle erstellen

Für mein Pi Fusion Projekt habe ich folgende Funktion in Raspcontrol gefunden, die aus einer Textdatei (bzw. aus einem String oder einer Standardausgabe bei Linux) eine HTML-Tabelle macht. Ich habe diese Funktion für meine Bedürfnisse leicht modifiziert. Die Ausgabe funktioniert gut, habe aber diese Funktion ausgelagert, da ich diese Funktion bereits durch eine andere ersetzt habe. Die TXT-Datei ($txtFile) muss eine Spaltenüberschrift haben, damit die Funktion auch korrekt arbeitet.

// function by raspcontrol - modified by Andy_P
function txt_to_html_table($txtFile)
{
  $txtFile = preg_split('/[\r\n]+/', $txtFile);

  // remove double (or more) spaces for all items
  foreach ($txtFile as &$item) {
    $item = preg_replace('/[[:blank:]]+/', ' ', $item);
    $item = trim($item);
  }

  // remove empty lines
  $txtFile = array_filter($txtFile);

  // the first line contains titles
  $columnCount = preg_match_all('/\s+/', $txtFile[0]);
  $txtFile[0] = '<tr><th>' . preg_replace('/\s+/', '</th><th>', $txtFile[0], $columnCount) . '</th></tr>';
  $tableHead = $txtFile[0];
  unset($txtFile[0]);

  // others lines contains table lines
  foreach ($txtFile as &$item) {
    $item = '<tr><td>' . preg_replace('/\s+/', '</td><td>', $item, $columnCount) . '</td></tr>';
  }

  // return the build table
  return '<table class=\'table table-striped\'>'
        . '<thead>' . $tableHead . '</thead>'
        . '<tbody>' . implode($txtFile) . '</tbody>'
      . '</table>';
}

Beispiel:

$storageDetails = shell_exec('df -lTh');	
$result = txt_to_html_table($storageDetails);

So sieht dann die Ausgabe der Tabelle nach einigen kosmetischen Veränderungen aus:


Credit: Raspcontrol, Jacob Clark (Bioshox), GNU GPL 2.0