如何打印使用php代码生成的文本文件的特定部分?

问题描述 投票:0回答:1

对于该项目,我正在研究,我需要在特定标题下打印文本文件的特定部分。索引文件要求提供三个输入,并将它们全部放入以“,”分隔的文本文件行。三个输入中的最后一个是文本区域,而不是简单的输入框。并且每个新条目都打印在新行上

例如,这是文本文件所说的:

John, Appleseed, This is a test for the first line that is printed
Charles, Lee, This is a test for the second line that is printed
etc.

我已经完成了这么多,令我感到困惑的是如何使文本文件像这样打印,在使用这些值之前,它会显示“ first name:”,“ last name:”和“ comment:”在文本文件中

原样

First Name: John
Last Name: Appleseed
Comment: This is a test for the first line that is printed

并且它将继续打印这些语句,直到使用了文本文件的所有行为止

php arrays text-files
1个回答
0
投票

您可以逐行读取文件并以逗号解析行。每行有三个项目名称,姓氏和注释。所以;

$file = fopen(__DIR__ . '/comments.txt', 'r');

while (($line = fgets($file)) !== false) {
    [$name,$surname,$comment] = explode(",", $line);
    // then use above variables to achieve what ever you want
    echo "First Name :" . $name;
    echo "Last Name  :" . $surname;
    echo "Comment    :" . $comment;
}

fclose($file);
© www.soinside.com 2019 - 2024. All rights reserved.