【问题标题】:How can i use PHP explode to create HTML table from text file with a space in between each word我如何使用 PHP 爆炸从文本文件中创建 HTML 表格,每个单词之间有一个空格
【发布时间】: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>&nbsp;</td>"; $tdcount++;
             } $str .= "</tr>";
         }
         $str .= "</tbody></table>";
         echo $str;
         exit;

?>

我尝试通过将 , 更改为空格来玩爆炸线,但它不起作用:

$arrM = explode(" ",fgets($f));

我该怎么做?

【问题讨论】:

    标签: php arrays explode


    【解决方案1】:

    与其读取每一行然后分解它,不如将其读取为带有空格分隔符的 CSV 文件(使用fgetcsv()),确保每一行都有 3 个结果(尽管它不能满足更多)然后@ 987654322@ 每一行都需要&lt;td&gt; 标记并将其添加到结果中...

    $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>
    

    【讨论】:

      【解决方案2】:

      假设文件中的行确实由空格分隔,以下应该可以工作并创建您正在寻找的内容。

      ?>
      <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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-28
        • 2014-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-07
        • 1970-01-01
        相关资源
        最近更新 更多