我在读取和验证某些文件(.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
我被卡住了,我不知道如何读取第一行,然后再从第二行读取其余内容。我必须循环执行所有这些操作才能读取多个文件。我设法阅读并验证了日期的第一行,因为我也不知道该怎么做。但是现在我需要从第二行开始阅读,而无需第一行。 这是我刚开始以程序员的身份就读的学校项目之一,如果有人可以帮助我,请。
<?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();
?>
答案 0 :(得分:0)
最简单的方法是打开文件,读取第一行-验证是否有效,然后在文件结束之前读取每一行并验证这些行。还可以使用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