【问题标题】:Parse txt files and turn them into static html files解析txt文件并将其转成静态html文件
【发布时间】:2016-02-24 08:46:30
【问题描述】:

更新(2015 年 11 月 24 日)

除了一个小细节外,我的一切都正常工作。我需要弄清楚如何获取 HTML 模板中的静态占位符变量,以便我可以用从 TXT 文件中提取的内容替换它们。

这是我的模板代码:

<!DOCTYPE html>
<html>
<head>
  <title>{PAGE_TITLE}</title>
</head>
<body>
  {PAGE_TITLE} - {PAGE_AUTHOR} - {PAGE_DATE}
  {PAGE_CONTENT}
</body>
</html>

原创

我查看了这个问题PHP - parsing a txt file 并尽可能自己解决问题。

出于教育目的,我正在用 PHP 创建一个简单、非常小的静态站点生成器。我有一个包含单个 PHP 文件的目录,其中包含所有代码(HTML 模板除外),它将扫描当前目录中的任何 txt 文件并确定是否有多个文件,因此可以使用循环处理每个文件。

我的 txt 文件的结构如下:

TITLE
AUTHOR
DATE

Text starts here...

我被困在从文件中提取 TITLE、AUTHOR、DATE 和文本内容并将它们存储在正确的变量中以便可以将信息传递给 HTML 模板进行处理的部分。

我也想设置它,这样当有换行符/回车时,它会在该文本块上附加一个 HTML 段落标签。

这是我目前为 PHP 文件编写的代码:

<?php
$files = glob("*.txt"); // Scan directory for .txt files

// Check that there are .txt files in directory
if ($files !== false) {
    $numberOfFiles = count($files); // Count number of .txt files in directory

    // Check if number of files is greater than one
    if ($numberOfFiles > 1) {
        // Advanced loop will go here to process multiple txt files
    } else {
        $file_handle = fopen ($files[0], "r"); // Open file

        // Loop through file contents line-by-line
        while (!feof ($file_handle)) {
            $file = file_get_contents($files[0]); // Get file contents
            $rows = explode ("\n", $file); // Count number of rows in file

            // Need to pull TITLE, AUTHOR, and DATE from txt file
            // Here's where I need the rest of the file's content to be parsed into paragraph blocks for the html template

            break; // Break loop after one run
        }

        fclose ($file_handle); // Close file connection
    }
}
?>

【问题讨论】:

  • 如果标题、作者和日期始终在前三行,您可以使用 $rows[0]、$rows[1] 和 $rows[2] 将它们取出。要获取您的文本,您可以在之后从数组中删除前三个元素,然后将它们全部重新组合在一起,再次包含在段落标签中。
  • 也许每个项目一行一行的 csv 类型文件可能比多个单独的文件更容易管理?
  • Dontfeedthecode 的方法看起来也是一种很好且有效的方法。
  • 基于 Dontfeedthecode 的数组建议的全部返工 - 太好了!
  • @subless - 不知道你是否还在做这个,但我认为这最终是你真正想要的!请告诉我。

标签: php html


【解决方案1】:

您可以从文件中逐行获取,而不是获取整个文件,然后单独格式化它们并将它们放入准备好在页面上回显的变量中。但是,Dontfeedthecode 提出的方法到目前为止更优越,效率更高,我已经撤回了原来的想法,希望他会认可我对他的想法所做的事情。

     <?php         
      $files = glob("*.txt"); // Scan directory for .txt files

      // Check that there are .txt files in directory
           if ($files !== false) {
           $numberOfFiles = count($files); // Count number of .txt files in directory

              // Check if number of files is greater than one
              if ($numberOfFiles > 1) {
              // Advanced loop will go here to process multiple txt files
              } else {

              $text_array = array();
              $file_handle = fopen ($files[0], "r"); // Open file
              $text_array = stream_get_contents($file_handle);
              $text_array = explode("\n", $text_array);
              // get the top three lines
              $page_title = trim($text_array[0]);
              $all_lines = '<p>' .  trim($text_array[0]) . ' - ' . trim($text_array[1]) .  ' - ' . trim($text_array[2]) . '</p>';
              // delete the top four array elements
              $text_array[0] = $text_array[1] = $text_array[2] = $text_array[3] = '';
             // get the remaining text
              $text_block =  trim(implode($text_array));
              fclose ($file_handle); // Close file connection
         }  // endifs for first if(... statements
     }
     ?>

HTML 输出:

         <!DOCTYPE html>
         <html>
            <head>
               <title><?php echo $page_title; ?></title>
            </head>
                    <body>
                      <?php echo $all_lines . "\n" . '<p>' . $text_block .'</p>'. "\n"; ?>
                    </body>
         </html>


A variable ready to print to file:


         <?php
                   $print_to_file = '<!DOCTYPE html>
               <html>
                     <head>
                           <title>' . $page_title . '</title>
                     </head>
                       <body>' . "\n"  . $all_lines . "\n" . '<p>' . $text_block .'</p>'. "\n" .
                       '     </body>
          </html>';

         echo $print_to_file;
         ?>

这里的变量中的 HTML 看起来有点错位,但在打印时就出来了。

最后,一个版本为文本的每一行放置一个&lt;p&gt; 标签。

     <?php
     $files = glob("*.txt"); // Scan directory for .txt files

    // Check that there are .txt files in directory
     if ($files !== false) {
     $numberOfFiles = count($files); // Count number of .txt files in directory

         // Check if number of files is greater than one
         if ($numberOfFiles > 1) {
         // Advanced loop will go here to process multiple txt files
         } else {

         $text_array = array();
         $file_handle = fopen ($files[0], "r"); // Open file

         $text = stream_get_contents($file_handle);

         // get the top three lines
         $text_array = explode("\n", $text);
         $page_title = trim($text_array[0]);
         $all_lines = '<p>' .  $text_array[0] . ' - ' . $text_array[1] .  ' - ' . $text_array[2] . '</p>';
         // set up something to split the lines by and add the <p> tags
         $text_array = str_replace("\n","</p>\nxxx<p>", $text);
         $text_array = explode("xxx", $text_array);

         // delete the top four array elements
         $text_array[0] = $text_array[1] = $text_array[2] = $text_array[3] = '';
         // get the remaining text



         $text_block =  trim(implode($text_array));

         }
     }
     ?>

这个版本可以使用和上面一样的html/php块

【讨论】:

    猜你喜欢
    • 2013-07-11
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 2016-10-29
    相关资源
    最近更新 更多