首页 > 解决方案 > 如何从文本文件 validate 中读取第一行,然后将文件的其余部分读入数组?

问题描述

我在读取和验证某些文件(.txt)时遇到问题。我要做的是从文本中读取第一行,它有 4 列:模块名称、标题名称、教师姓名和日期。我需要将第一行转换为字符串并验证每一列。验证后继续读取下一行的文件,该文件有 2 列,一列用于 id,一列用于标记。我还需要使用第二行并验证 id 和标记。

文本文件就是这种格式。其中一些缺少日期或名称。

DT1617T1, Problem Solving for Programming , Gordon MacIntyre  , 20/05/2016
12345678, 56
34567822, 67
12324654, 98
234769O1, 45
12563792, 49
74537299, 7I
99834511, 50
77625489, 56
55274559, 63
22009643, 71
72578129, 51

我被卡住了,我不知道如何读取第一行 validate 然后从第二行读取其余部分。我必须循环执行所有这些操作才能读取多个文件。我设法阅读并验证了日期的第一行,因为我也不知道该怎么做。但现在我需要从没有第一行的第二行开始阅读。这是我刚以程序员身份开始的学校项目之一,如果有人可以帮助我,请。

<?php 
function sorting(){
$dataFolder = 'data';
// checking if directory with the name 'data' exists
     if(is_dir($dataFolder)){  
          $textFiles = glob("$dataFolder/*.txt"); //extracting the files with the   extension .txt


              $i=0;//counter for the files array;
              $errorCount=0;//counting error in the first line of text;
              //sorting the .txt files and extracting the first line from file
              foreach($textFiles as $files){ //looping trought the folder and extracting all the files with extension .txt
                 $codeLines = fopen($files, "r");//opening the files

                 $fileContets=file_get_contents($files);//getting all content of the file for validation
                 $fLine=fgets($codeLines) ;//reading the files
                 $fLine=explode("," , $fLine);//storring the first line of text as an array
                 fclose($codeLines);//closing the file
                   //validation function
                  if(filesize($files) == 0 or $fileContets == "" ){ echo "<p>Ups the file is empty</p>";}

                     $moduleCode =  $fLine[0]; //first item in the array $fLine
                     $moduleTitle = $fLine[1]; //second item in the array $fLine
                     $tutorName =   $fLine[2]; //third item in the array $fLine
                     $dateMarking = $fLine[3]; //forth item in the array $fLine

                  $errorLog="<p>Module Code: $moduleCode- ERROR Module code is incorect</p>"; //error code
                  $x = "<p>Module Code: $moduleCode </p>";// no error


                  echo "<p>File name: $textFiles[$i]  </p>"; // echo the file name that is currently itterate

                  $i++;


                  if (!preg_match('/^(PP|P1|DT)[\d]{4}T[123]/' , $moduleCode)) {
                       $errorCount++;                       
                       echo $errorLog; }
                  else  
                        {echo " <p>Module Code: $moduleCode </p>" ; }

                  if($moduleTitle == "")
                        {echo "<p>Title:  ERROR -  Title is missig </p>"; $errorCount++;}
                  else
                        {echo "<p>Title: $moduleTitle </p>"; }

                  if($tutorName == "")
                        {echo "<p>Tutor Name: ERROR - Tutor name is missing </p>"; $errorCount++;}
                  else 
                        {echo "<p>Tutor Name: $tutorName</p>";}


                      $handle = fopen($files  , 'r');
                              while (!feof($handle)) {
                        $name = fgets($handle, 1024);
                         echo '<p>' . $name . '</p>'; }


                  }

                        echo"<p>Numbers of errors on file is : `enter code here`$errorCount</p>";


                    }

                  }

     sorting();
?>

标签: php

解决方案


最简单的方法是打开文件,读取第一行 - 验证,如果这是有效的,则在文件结束后读取每一行并验证这些行。也用于fgetcsv()读取文件,它为您提供文件中字段的数组,而无需使用explode().

使用更多类似...的结构可能会更好

          foreach($textFiles as $files){ //looping trought the folder and extracting all the files with extension .txt
             $codeLines = fopen($files, "r");//opening the files

             $fLine=fgetcsv($codeLines) ;//reading the files

             // Validate the 1st line

             if ( !$valid ) {
                 fclose ( $codeLines );
                 continue;    // Skip rest of read
             }

             while ( !feof($codeLines) ) {  // Loop to end of file
                 $fLine = fgetcsv($codeLines) ;

                 // Validate the rest of the lines

             }
             fclose ( $codeLines );   // When finished close the file

推荐阅读