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




Leuchtdioden (LEDs) mit HTML / CSS anzeigen

Hier eine Möglichkeit LEDs mit HTML und CSS zu realisieren:


HTML

Die HTML Container für die Anzeige der jeweiligen LED-Farbe:

<div class="led-red"></div>
<div class="led-yellow"></div>
<div class="led-green"></div>
<div class="led-blue"></div>
<div class="led-grey"></div>

CSS

Die entprechende CSS-Datei für die LEDs:

.led-red {
    margin: 5px auto;
    width: 12px;
    height: 12px;
    background-color: #940;
    border-radius: 50%;
    box-shadow: #000 0 -1px 7px 1px, inset #600 0 -1px 9px, #F00 0 2px 12px;
}

.led-yellow {
    margin: 5px auto;
    width: 12px;
    height: 12px;
    background-color: #A90;
    border-radius: 50%;
    box-shadow: #000 0 -1px 7px 1px, inset #660 0 -1px 9px, #DD0 0 2px 12px;
}

.led-green {
    margin: 5px auto;
    width: 12px;
    height: 12px;
    background-color: #690;
    border-radius: 50%;
    box-shadow: #000 0 -1px 7px 1px, inset #460 0 -1px 9px, #7D0 0 2px 12px;
}

.led-blue {
    margin: 5px auto;
    width: 12px;
    height: 12px;
    background-color: #4AB;
    border-radius: 50%;
    box-shadow: #000 0 -1px 7px 1px, inset #006 0 -1px 9px, #06F 0 2px 14px;
}

.led-grey {
    margin: 5px auto;
    width: 12px;
    height: 12px;
    background-color: #C0C0C0;
    border-radius: 50%;
    box-shadow: #000 0 -1px 7px 1px, inset #9A9 0 -1px 9px, #969 0 2px 14px;
}

Die Farbe grau habe ich selbst erstellt.

Die CSS-Datei im HTML Header laden:

<head>
....
<link rel="stylesheet" href="path_to_your_css/led.css">
</head>