【发布时间】:2019-10-12 12:36:10
【问题描述】:
我有一个包含 3 列的文本文件,其中包含以下内容:
word1 word2 word3
word4 word5 word6
word7 word8 word9
我有以下 PHP 将我的文本文件转换为表格,但是它将文本文件的每一行放入一个表格单元格中,我希望它将每个单词放入一个表格单元格中。
<?php
$tdcount = 1; $numtd = 3; // number of cells per row
$str = "<table class=\"table table-hover table-striped\">
<thead>
<th>colum1</th>
<th>colum2</th>
<th>colum3</th>
</thead>
<tbody>
";
$f = fopen("/text.txt", "r");
if ( $f === FALSE ) {
exit;
}
while (!feof($f)) {
$arrM = explode(",",fgets($f));
$row = current ( $arrM );
if ($tdcount == 1)
$str .= "<tr>"; $str .= "<td>$row </td>";
if ($tdcount == $numtd) {
$str .= "</tr>";
$tdcount = 1;
} else {
$tdcount++;
}
}
if ($tdcount!= 1) {
while ($tdcount <= $numtd) {
$str .= "<td> </td>"; $tdcount++;
} $str .= "</tr>";
}
$str .= "</tbody></table>";
echo $str;
exit;
?>
我尝试通过将 , 更改为空格来玩爆炸线,但它不起作用:
$arrM = explode(" ",fgets($f));
我该怎么做?
【问题讨论】: