【问题标题】:Read each line of text of each file in a directory and place into array?读取目录中每个文件的每一行文本并放入数组中?
【发布时间】:2013-09-09 18:53:50
【问题描述】:

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

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

我创建了一个网站来帮助一些在家上学的学生,但想将此功能添加到该网站以帮助他们查看过去、现在和未来的所有课程。我知道一点 PHP,但无法理解它,似乎我尝试得越多,我就越感到困惑。这对我来说是一次学习经历。

我尝试过使用fopen,但只能打开一个文本文件而不是整个目录。我在想我需要获取一个目录列表,将其放入一个数组中,然后使用fopen 打开数组中的每个文件,但我可能还差得很远。非常感谢任何帮助我指明正确方向的帮助!

【问题讨论】:

  • 向我们展示您尝试的代码。当她询问时试图弄清楚女人的胸罩或内衣颜色时,猜测很有趣,但这是不同的。 ;-)

标签: php arrays text-files


【解决方案1】:

您的方法是一种方法。您可以扫描目录中所需的文件,并使用file() 函数检索数组中的文件内容。我只会发布部分代码,因为从目录中获取文件名是显而易见的(请参阅其他答案中的 glob())。

//从数组中的给定目录获取文件列表(数组将包含文件名)。 //建议文件名使用完整路径或脚本的相对路径

$task_array = Array();

foreach ($filelist as $filename)
{
    try
    {
        $file_content = file($filename); // we get an array with this function
        // you could do this the other way, by using fopen() and fread(), but this is easier
    }
    catch(Exception $e)
    {
        $(file_content = false;
    }

    if (($file_content !== false) && (!empty($file_content)))
    {
        $task_array[] = $file_content;
    }
}

你的任务数组会变成一个二维数组,像这样:

Array(

    [0] -> Array(
        [0] -> 1
        [1] -> 'Lesson Title'
        [2] -> 'Lesson Description here'
        [3] -> '2013-09-25'
    )

    [1] -> Array(
        [0] -> 2
        [1] -> 'Lesson Title 2'
        [2] -> 'Lesson 2 Description here'
        [3] -> '2013-09-25'
    )
)

然后,当你有这个数组时,你可以再次使用 foreach,以 HTML 显示它。

但是,如果您想以正确的方式执行此操作,则应使用数据库,例如 MySQL。

【讨论】:

    【解决方案2】:

    您可以采用两种方式,要么使用 glob 扫描目录,要么使用更好的 Directory Iterator http://php.net/manual/en/class.directoryiterator.php,我建议使用 glob 方法更容易一些,所以我不喜欢它。

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

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

    <?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>';
    
    ?>
    

    【讨论】:

    • 这完美地为每个文件的每一行创建了一个数组,并帮助我指出了我需要的地方!代码中的 cmets 对我帮助很大。
    • 我首先要添加几个其他指针,这将按照目录中文件结构的顺序对文件进行排序 - 所以通常 a-z 可以更改 - 让我知道并编辑我的答案
    • 我确实更改了订单,将foreach (glob($directory ... 更改为foreach (array_reverse(glob($directory ... 再次感谢您的帮助!
    【解决方案3】:

    这就是我遍历目录和读取文件的方式:

    $dir = "/YOUR_DIRECTORY_PATH/*";
    
    foreach(glob($dir) as $file) { //load each file in the directory
    
        $fileHandle = fopen($file, "r");
    
        while (!feof($fileHandle)) {   // load each line
            $line = fgets($fileHandle);
            echo $line . '<br />';
        }
        fclose($fileHandle);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多