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

时间:2015-11-22 01:59:03

标签: php html

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

ORIGINAL

我回顾了这个问题PHP - parsing a txt file,并尽我所能。

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

我的txt文件的结构如下:

TITLE
AUTHOR
DATE

Text starts here...

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

我还希望设置它,所以当有换行/返回时,它会在该文本块中附加一个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
    }
}
?>

1 个答案:

答案 0 :(得分: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块