将txt文件解析并转换为静态html文件

3

更新(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...

我卡在了从文件中提取标题、作者、日期和文本内容并将它们存储在正确的变量中,以便信息可以传递到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
    }
}
?>

2
如果标题、作者和日期总是在前三行,您可以使用 $rows[0]、$rows[1] 和 $rows[2] 将它们提取出来。要获取文本,您可以从数组中删除这前三个元素,然后将其全部合并在一起,用段落标签括起来。 - Tim Sheehan
1
也许使用每行一个条目的 CSV 类型文件比多个单独的文件更容易管理? - Steve
1
Dontfeedthecode的方法看起来也是一个不错且有效的方式。 - Steve
根据Dontfeedthecode的数组建议进行了全面改进 - 简直太棒了! - Steve
@subless - 不知道你是否还在继续这个项目,但我认为这可能是你真正想要的!请让我知道。 - Steve
1个回答

3
您可以逐行读取文件中的内容,而不是获取整个文件,然后逐个格式化并将它们放入变量中,以便在页面上回显。然而,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代码看起来有些不一致,但在打印时会正确显示。

最后,有一个版本将文本的每一行都放在一个<p>标签内。

     <?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块。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接