我如何使用 PHP explode 从文本文件创建 HTML table,每个单词之间有一个 space
How can i use PHP explode to create HTML table from text file with a space in between each word
我有一个包含 3 列的文本文件,其中包含以下内容:spaces:
word1 word2 word3
word4 word5 word6
word7 word8 word9
我有以下 PHP 将我的文本文件转换为 table,但是它将文本文件的每一行放入一个 table 单元格中,我希望它能每个单词放入 table 个单元格。
<?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;
?>
我尝试通过将 , 更改为 space 来玩爆炸线,但是它不起作用:
$arrM = explode(" ",fgets($f));
我该怎么做?
假设文件中的行确实由空格分隔,以下内容应该可以工作并创建您要查找的内容。
?>
<table class="table table-hover table-striped">
<thead>
<th>colum1</th>
<th>colum2</th>
<th>colum3</th>
</thead>
<tbody>
<?php
while (!feof($f)) {
$arrM = explode(" ",fgets($f));
$row = current ( $arrM );
echo '<tr>';
foreach ($arrM as $cell) {
echo '<td>' . $cell . '</td>';
} // end of the foreach
echo '</tr>';
} // end of the while
echo '</tbody></table>';
exit;
不是读取每一行然后展开它,而是将其作为带有 space 分隔符的 CSV 文件读取(使用 fgetcsv()
),确保每一行都有 3 个结果(尽管它没有'满足更多)然后 implode()
每行需要 <td>
标签并将其添加到结果中...
$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 ( $row = fgetcsv($f, null, " ") ) {
// Ensure all rows have at least 3 cells
$row = array_pad($row, 3, "");
// Output data in a row
$str .= "<tr><td>".implode("</td><td>", $row). "</td></tr>".PHP_EOL;
}
$str .= "</tbody></table>";
echo $str;
其中(带有额外的部分数据记录)给出...
<table class="table table-hover table-striped">
<thead>
<th>colum1</th>
<th>colum2</th>
<th>colum3</th>
</thead>
<tbody>
<tr><td>word1</td><td>word2</td><td>word3</td></tr>
<tr><td>word4</td><td>word5</td><td>word6</td></tr>
<tr><td>word7</td><td>word8</td><td>word9</td></tr>
<tr><td>word10</td><td></td><td></td></tr>
</tbody></table>
我有一个包含 3 列的文本文件,其中包含以下内容:spaces:
word1 word2 word3
word4 word5 word6
word7 word8 word9
我有以下 PHP 将我的文本文件转换为 table,但是它将文本文件的每一行放入一个 table 单元格中,我希望它能每个单词放入 table 个单元格。
<?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;
?>
我尝试通过将 , 更改为 space 来玩爆炸线,但是它不起作用:
$arrM = explode(" ",fgets($f));
我该怎么做?
假设文件中的行确实由空格分隔,以下内容应该可以工作并创建您要查找的内容。
?>
<table class="table table-hover table-striped">
<thead>
<th>colum1</th>
<th>colum2</th>
<th>colum3</th>
</thead>
<tbody>
<?php
while (!feof($f)) {
$arrM = explode(" ",fgets($f));
$row = current ( $arrM );
echo '<tr>';
foreach ($arrM as $cell) {
echo '<td>' . $cell . '</td>';
} // end of the foreach
echo '</tr>';
} // end of the while
echo '</tbody></table>';
exit;
不是读取每一行然后展开它,而是将其作为带有 space 分隔符的 CSV 文件读取(使用 fgetcsv()
),确保每一行都有 3 个结果(尽管它没有'满足更多)然后 implode()
每行需要 <td>
标签并将其添加到结果中...
$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 ( $row = fgetcsv($f, null, " ") ) {
// Ensure all rows have at least 3 cells
$row = array_pad($row, 3, "");
// Output data in a row
$str .= "<tr><td>".implode("</td><td>", $row). "</td></tr>".PHP_EOL;
}
$str .= "</tbody></table>";
echo $str;
其中(带有额外的部分数据记录)给出...
<table class="table table-hover table-striped">
<thead>
<th>colum1</th>
<th>colum2</th>
<th>colum3</th>
</thead>
<tbody>
<tr><td>word1</td><td>word2</td><td>word3</td></tr>
<tr><td>word4</td><td>word5</td><td>word6</td></tr>
<tr><td>word7</td><td>word8</td><td>word9</td></tr>
<tr><td>word10</td><td></td><td></td></tr>
</tbody></table>