3

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 ("\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
    }
}
?>
subless
  • 79
  • 1
  • 8
  • 2
    If title, author and date are always on the first three lines you can pull them out with $rows[0], $rows[1] and $rows[2]. To get your text you can remove those first three elements from the array afterwards and implode it all back together again enclosed in paragraph tags. – Tim Sheehan Nov 22 '15 at 02:08
  • 1
    Perhaps a csv type file with one line per item might be easier to manage than multiple separate files? – Steve Nov 22 '15 at 03:00
  • 1
    Dontfeedthecode's method looks a good and efficient way to do it too. – Steve Nov 22 '15 at 05:38
  • Total rework based on Dontfeedthecode's array suggestion - it was just too good! – Steve Dec 01 '15 at 21:39
  • @subless - don't know if you are still working on this but I think this is finally what you really wanted! Please let me know. – Steve Dec 09 '15 at 20:37

1 Answers1

3

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("\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 Output:

         <!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 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("\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));

         }
     }
     ?>

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

Steve
  • 808
  • 1
  • 9
  • 14