读取目录中每个文件的每一行文本并放入数组吗? [英] Read each line of text of each file in a directory and place into array?

查看:42
本文介绍了读取目录中每个文件的每一行文本并放入数组吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录,其中包含文本文件,并且每天都会添加新的文本文件.每个文本文件都是一堂带有4行文本的学校课程.第1行是课程编号,第2行是课程标题​​,第3行是Description,第4行是截止日期.

I have a directory with text files in it and new text files getting added each day. Each text file is a school lesson with 4 lines of text. Line 1 is Lesson Number, line 2 is Lesson Title, line 3 is Description, and line 4 is Due Date.

在PHP中,我需要能够读取所有当前和将来的文本文件并将它们放入HTML表中.表格中的4列分别标记为课程编号",课程标题",描述"和截止日期".每个文本文件都是1个表格行.

I need, in PHP, to be able to read all current and future text files and place them into an HTML table. 4 columns in a table labeled Lesson Number, Lesson Title, Description, and Due Date. Each text file being 1 table row.

我已经创建了一个网站来帮助一些在家学习的学生,但是我想将此功能添加到该网站中以帮助他们查看过去,现在和将来的所有课程.我知道一些PHP,但是无法解决这个问题,而且尝试的次数越多,我就越感到困惑.对我来说这是一次学习经历.

I've made a site to help out some homeschooled students but wanted to add this functionality to the site to help them view all past, present, and future lessons. I know a little PHP but can't wrap my head around it and it seems the more I try the more I'm getting confused. This is a learning experience for me.

我尝试使用 fopen ,但只能获取它来打开文本文件,而不是整个目录.我当时想我需要获取一个目录列表,将其放入数组中,并使用 fopen 打开数组中的每个文件,但我可能会离开.非常感谢您为我指明正确的方向!

I've tried using fopen but can only get it to open a text file and not a whole directory. I was thinking I need to get a directory listing, place that into an array, and use fopen to open each file in the array but I may be way off. Any help to point me in the right direction is greatly appreciated!

推荐答案

您可以通过两种方式使用glob来扫描目录,或者使用更好的Directory Iterator

You could go two ways with this either go with glob which will scan the directory or the better Directory Iterator http://php.net/manual/en/class.directoryiterator.php which i would recommend with the glob method is a bit easier so ill go with that.

这取决于您是否已经存储了以前的记录,但是无论哪种方式都请尝试在此处获取一些示例.

It depends with you are already storing the previous records or not but either way ill try to get some examples on here.

我现在已经改进并测试了以下代码,因为我写的另一个是垃圾

I have now improved and tested the below code because the other one i wrote was rubbish

<?php

/**
 * @author - Sephedo
 * @for - Randall @ Stackoverflow
 * @question - http://stackoverflow.com/questions/18704981/read-each-line-of-text-of-each-file-in-a-directory-and-place-into-array/18705231#18705231
 */

$directory = 'lessons/'; // The directory to the lesson text files

$linesToReturn = 4; // Set to four for the number of lines in each text file ( for expansion? )

// Find all files in the directory which are .txt files
foreach( glob( $directory . "*.txt" ) as $filename )
{
    // Open the file
    if( $handle = @fopen( $filename, "r") )
    {
        $x = 0; // Start the line counter

        // Cycle each line until end or reach the lines to return limit
        while(! feof( $handle ) or $x < $linesToReturn )
        {
            $line = fgets($handle); // Read the line

            $lessons[$filename][] = $line;

            $x++; // Increase the counter
        }

        // This lines makes sure that are exactly 4 lines in each lesson
        if( count( $lessons[$filename] ) != $linesToReturn ) unset( $lessons[$filename] );
    }
}

// creates a blank list if no files or valid files were found.
if(! isset( $lessons ) ) $lessons = array();

// The rest of the page just builds a simple table to display each lesson.-
echo '<h1>Lesson Plans</h1>';
echo '<table>';
echo '<th>Lesson Number</th><th>Lesson Title</th><th>Description</th><th>Due Date</th>';

foreach( $lessons as $file => $details )
{
    echo '<tr><td>' . $details[0] . '</td><td>' . $details[1] . '</td><td>' . $details[2] . '</td><td>' . $details[3] . '</td></tr>';
}

echo '</table>';

?>

这篇关于读取目录中每个文件的每一行文本并放入数组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆