在PHP中创建具有任意行数的HTML表

在PHP中创建具有任意行数的HTML表,php,html,Php,Html,我对PHP知之甚少,因此决定创建一个基于web的工具来生成Red Hat kickstart文件,这将是一个很好的学习项目。除此之外,该工具将解析CSV文件并生成一个包含从中获取的数据的表。输入文件的格式如下: host1,10.153.196.248,255.255.255.0,10.153.196.1,00:50:56:ac:69:cb ,10.153.157.113,255.255.255.128,10.153.157.1, ,10.153.157.241,255.255.255.128,

我对PHP知之甚少,因此决定创建一个基于web的工具来生成Red Hat kickstart文件,这将是一个很好的学习项目。除此之外,该工具将解析CSV文件并生成一个包含从中获取的数据的表。输入文件的格式如下:

host1,10.153.196.248,255.255.255.0,10.153.196.1,00:50:56:ac:69:cb ,10.153.157.113,255.255.255.128,10.153.157.1, ,10.153.157.241,255.255.255.128,10.153.157.129, ,/home,10,,, ,swap,10,,, ,/opt,60,,, ,/data,30,,, ,,,,, host2,10.153.155.124,255.255.255.128,10.153.155.1,00:50:56:ac:69:ce ,10.153.157.114,255.255.255.128,10.153.157.1, ,10.153.157.242,255.255.255.128,10.153.157.129, ,/home,10,,, ,swap,10,,, ,/opt,60,,, ,/data,30,,, ,,,,, host1,10.153.196.248255.255.0,10.153.196.1,00:50:56:ac:69:cb ,10.153.157.113,255.255.255.128,10.153.157.1, ,10.153.157.241,255.255.255.128,10.153.157.129, ,/家,10,,, 交换,交换,交换,交换,,, ,/opt,60,,, ,/数据,30,,, ,,,,, host2,10.153.155.124255.255.255.128,10.153.155.1,00:50:56:ac:69:ce ,10.153.157.114,255.255.255.128,10.153.157.1, ,10.153.157.242,255.255.255.128,10.153.157.129, ,/家,10,,, 交换,交换,交换,交换,,, ,/opt,60,,, ,/数据,30,,, ,,,,, 文本的每个部分表示一台服务器的信息。字段如下所示:

hostname,eth0 IP, eth0 netmask, eth0 gateway, eth4 MAC null,eth1 IP, eth1 netmask, eth1 gateway, null blank,eth2 IP, eth2 netmask, eth2 gateway, null null,partition name, partition size in GB, null, null null,partition name, partition size in GB, null, null null,partition name, partition size in GB, null, null null,partition name, partition size in GB, null, null null,null,null,null,null 主机名、eth0 IP、eth0网络掩码、eth0网关、eth4 MAC null,eth1 IP,eth1网络掩码,eth1网关,null 空白,eth2 IP,eth2网络掩码,eth2网关,null null,分区名称,分区大小(GB),null,null null,分区名称,分区大小(GB),null,null null,分区名称,分区大小(GB),null,null null,分区名称,分区大小(GB),null,null 空,空,空,空,空,空 目前,我可以解析它并生成一个表,其中输入文件中的每一行都是表中的一行。处理此问题的函数:

function processFile($workFile) {

    if (file_exists($workFile)) {
        print '<table>';
        $fh = fopen("$workFile", 'rb');
        if ($fh) {
            for ($line = fgets($fh); !feof($fh); $line = fgets($fh)) {
                $line = trim($line);
                $info = explode(',', $line);
                print '<tr><td>' . $info[0] . '</td><td>' . $info[1] . '</td><td>' . $info[2] . '</td><td>' . $info[3] . '</td></tr>';
            }
        } else {
            print "Failed to open $workFile";
        }
        print '</table>';
    } else {
        print "File $workFile does not exist";
    }

}
函数processFile($workFile){
如果(文件_存在($workFile)){
打印“”;
$fh=fopen($workFile,'rb');
若有($fh){
对于($line=fgets($fh);!feof($fh);$line=fgets($fh)){
$line=修剪($line);
$info=分解(“,”,$line);
打印“.$info[0]”.$info[1]”.$info[2]”.$info[3]”;
}
}否则{
打印“打开$workFile失败”;
}
打印“”;
}否则{
打印“文件$workFile不存在”;
}
}
由此产生:

host1 eth0 IP eth0 netmask eth0 gateway eth1 IP eth1 netmask eth1 gateway eth2 IP eth2 netmask eth2 gateway partition 1 partition 1 size partition 2 partition 2 size partition 3 partition 3 size partition 4 partition 4 size host2 eth0 IP eth0 netmask eth0 gateway eth1 IP eth1 netmask eth1 gateway eth2 IP eth2 netmask eth2 gateway partition 1 partition 1 size partition 2 partition 2 size partition 3 partition 3 size partition 4 partition 4 size 主机1 eth0 IP eth0网络掩码eth0网关 eth1 IP eth1网络掩码eth1网关 eth2 IP eth2网络掩码eth2网关 分区1分区1大小 分区2分区2大小 分区3分区3大小 分区4分区4大小 主机2 eth0 IP eth0网络掩码eth0网关 eth1 IP eth1网络掩码eth1网关 eth2 IP eth2网络掩码eth2网关 分区1分区1大小 分区2分区2大小 分区3分区3大小 分区4分区4大小 这是一个开始。然而,并不是每台服务器都有四个分区。有些会有更多,有些会有一个或两个更少。提前不知道这些信息会妨碍我要做的事情,即在每个服务器的分区信息下面添加一行,并可能将每个服务器拆分为自己的表。大致如下:

host1 eth0 IP eth0 netmask eth0 gateway eth1 IP eth1 netmask eth1 gateway eth2 IP eth2 netmask eth2 gateway partition 1 partition 1 size partition 2 partition 2 size partition 3 partition 3 size partition 4 partition 4 size partition 5 partition 5 size partition 6 partition 6 size How many disks? [Text Field} host2 eth0 IP eth0 netmask eth0 gateway eth1 IP eth1 netmask eth1 gateway eth2 IP eth2 netmask eth2 gateway partition 1 partition 1 size partition 2 partition 2 size partition 3 partition 3 size partition 4 partition 4 size How many disks? [Text Field} 主机1 eth0 IP eth0网络掩码eth0网关 eth1 IP eth1网络掩码eth1网关 eth2 IP eth2网络掩码eth2网关 分区1分区1大小 分区2分区2大小 分区3分区3大小 分区4分区4大小 分区5分区5大小 分区6分区6大小 有多少磁盘?[文本字段} 主机2 eth0 IP eth0网络掩码eth0网关 eth1 IP eth1网络掩码eth1网关 eth2 IP eth2网络掩码eth2网关 分区1分区1大小 分区2分区2大小 分区3分区3大小 分区4分区4大小 有多少个磁盘?[Text Field} 我的普遍想法是,我必须在CSV文件中有一个字段,表明该行包含分区信息。这似乎是最简单的方法。不过,我想知道是否还有其他方法可以使用,而不需要更改输入文件的格式

我还必须弄清楚如何使用包含所有空字段的行作为节分隔符

如有任何关于如何处理此问题的想法,我将不胜感激。

echo(“”);
echo('<table>');
$i = 0;
while($data = fgetcsv($fh)) {
    echo('<tr>');
    $cellTag = 'td';
    if($i == 0) {
        $cellTag = 'th';
    }
    $i++;
    foreach($data as $cell) {
        echo('<'.$cellTag.'>'.$cell.'</'.$cellTag.'>');
    }
    echo('</tr>');
}
echo('</table>');
$i=0; 而($data=fgetcsv($fh)){ 回声(“”); $cellTag='td'; 如果($i==0){ $cellTag='th'; } $i++; foreach($cell形式的数据){ 回声(“.$cell.”); } 回声(“”); } 回声(“”);

试一试。让我第一次知道它是否起作用。我本来会测试它,但我手头上没有CSV文件。

当您解析应该匹配特定格式的数据时,它应该是规则的,允许您使用正则表达式严格匹配所需的输入(并放弃您不需要的输入):


由于我们现在有一个结构正确的数据数组,所以以任何所需的格式(如HTML表格)输出数据应该很简单.

为什么不使用mysql数据库呢?可能数据是从其他来源导入的。该工具的目的是获取一个从现有电子表格创建的CSV文件,并使用它生成kickstart文件。生成文件后,CSV是不必要的。它可能会保留用于考古目的,b但否则,以数据库的形式进行持久化就太过分了。如果CSV丢失,可以从电子表格中重新创建它(如果需要的话)。正则表达式路径似乎比必要的复杂得多。我已经能够以我想要的格式输出数据。我只需要知道如何使用arbit每套分区的行数很少。我将研究您关于str_getcsv()的建议。这很有效。不过,我确实注释了创建表的部分
$rows = explode("\n", $input);

// @link http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address
$regIp = "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])";
$regHost = "(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])";

$hosts = array();
foreach ($rows as $row) {
  $matches = null;
  if (preg_match("/(?<host>$regHost),(?<ip>$regIp),(?<netmask>$regIp),(?<gateway>$regIp),(?<mac>.*)/", $row, $matches)) {
    $host = $matches['host'];
    $hosts[$host]['name'] = $matches['host'];
    $hosts[$host]['mac'] = $matches['mac'];
    $hosts[$host]['eth'][] = array(
      'ip'      => $matches['ip'],
      'netmask' => $matches['netmask'],
      'gateway' => $matches['gateway'],
    );
  } else if (preg_match("/,(?<ip>$regIp),(?<netmask>$regIp),(?<gateway>$regIp),/", $row, $matches)) {
    $hosts[$host]['eth'][] = array(
      'ip'      => $matches['ip'],
      'netmask' => $matches['netmask'],
      'gateway' => $matches['gateway'],
    );
  } else if (preg_match("/,(?<name>.+),(?<size>\d+),,,/", $row, $matches)) {
    $hosts[$host]['partition'][] = array(
      'name' => $matches['name'],
      'size' => $matches['size'],
    );
  } else if (preg_match("/,,,,,/", $row)) {
    // we already partition output array with value of `$host` variable.
    echo "Found terminating row\n"; 
  } else {
    echo "Unrecognised data on row: $row\n";
  }
}

var_export($hosts);
Found terminating row
Found terminating row
array (
  'host1' => 
  array (
    'name' => 'host1',
    'mac' => '00:50:56:ac:69:cb',
    'eth' => 
    array (
      0 => 
      array (
        'ip' => '10.153.196.248',
        'netmask' => '255.255.255.0',
        'gateway' => '10.153.196.1',
      ),
      1 => 
      array (
        'ip' => '10.153.157.113',
        'netmask' => '255.255.255.128',
        'gateway' => '10.153.157.1',
      ),
      2 => 
      array (
        'ip' => '10.153.157.241',
        'netmask' => '255.255.255.128',
        'gateway' => '10.153.157.129',
      ),
    ),
    'partition' => 
    array (
      0 => 
      array (
        'name' => '/home',
        'size' => '10',
      ),
      1 => 
      array (
        'name' => 'swap',
        'size' => '10',
      ),
      2 => 
      array (
        'name' => '/opt',
        'size' => '60',
      ),
      3 => 
      array (
        'name' => '/data',
        'size' => '30',
      ),
    ),
  ),
  'host2' => 
  array (
    'name' => 'host2',
    'mac' => '00:50:56:ac:69:ce',
    'eth' => 
    array (
      0 => 
      array (
        'ip' => '10.153.155.124',
        'netmask' => '255.255.255.128',
        'gateway' => '10.153.155.1',
      ),
      1 => 
      array (
        'ip' => '10.153.157.114',
        'netmask' => '255.255.255.128',
        'gateway' => '10.153.157.1',
      ),
      2 => 
      array (
        'ip' => '10.153.157.242',
        'netmask' => '255.255.255.128',
        'gateway' => '10.153.157.129',
      ),
    ),
    'partition' => 
    array (
      0 => 
      array (
        'name' => '/home',
        'size' => '10',
      ),
      1 => 
      array (
        'name' => 'swap',
        'size' => '10',
      ),
      2 => 
      array (
        'name' => '/opt',
        'size' => '60',
      ),
      3 => 
      array (
        'name' => '/data',
        'size' => '30',
      ),
    ),
  ),
)