Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

php - Parse txt files and turn them into static html files

UPDATE (11/24/2015)

I have everything working correctly except one small detail. I need to figure out how to get the static placeholder variables I have in my HTML template so that I can replace them with the content pulled from the TXT file.

Here's my template code:

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

ORIGINAL

I reviewed this question PHP - parsing a txt file and got as far as I could on my own.

I'm creating a simple, very small static site generator in PHP for educational purposes. I have a directory with a single PHP file which is where all the code will be (except for the HTML template), and it will scan the current directory for any txt files and decide if there's more than one so a loop can be used to process each file.

My txt file's are structured like so:

TITLE
AUTHOR
DATE

Text starts here...

I'm stuck on the part where I pull the TITLE, AUTHOR, DATE, and text content from the file and store them in their correct variables so that information can be passed to the HTML template to be processed.

I would also like to have it set up so when there is a newline/return it will append a HTML paragraph tag to that block of text.

Here's the code I have so far for the PHP file:

<?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 ("
", $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
    }
}
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could get the lines from the file one by one, instead of getting the whole file, and then formatting them individually and putting them into variables ready to be echoed out on your page. However, the method proposed by Dontfeedthecode is so far superior and more efficient that I have withdrawn the original and hope that he will approve of what I have done with his idea.

     <?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("
", $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 Output:

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


A variable ready to print to file:


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

         echo $print_to_file;
         ?>

HTML looks a bit displaced in the variable here but comes out right when printed.

And finally, a version which puts a <p> tag for each line of the text.

     <?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("
", $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("
","</p>
xxx<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));

         }
     }
     ?>

This version can use the same html/php blocks as above


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...