Ответ 1
Вы можете получить строки из файла один за другим, вместо того, чтобы получать весь файл, а затем форматировать их по отдельности и помещать их в переменные, готовые для эха на вашей странице. Тем не менее, метод, предложенный 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, что и выше