更新(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——解析一个txt文件,并尽可能地靠自己。
我正在用PHP创建一个简单、非常小的静态站点生成器,用于教育目的。我有一个目录,里面有一个PHP文件,所有代码都在那里(除了HTML模板),它会扫描当前目录中的任何txt文件,并决定是否有多个,这样就可以使用一个循环来处理每个文件。
我的txt文件的结构如下:
TITLE
AUTHOR
DATE
Text starts here...
我一直在从文件中提取TITLE、AUTHOR、DATE和文本内容,并将它们存储在正确的变量中,以便将信息传递给要处理的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
}
}
?>
您可以从文件中一行接一行地获取行,而不是获取整个文件,然后对它们进行单独格式化,并将它们放入变量中,以便在页面上进行回显。然而,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/p>