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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…