PHP脚本逐行读取txt文件,按照约定的格式对其进行排序并计算统计信息

时间:2016-11-14 09:48:13

标签: php function

bbm.txt文件包含:

BBM, PHP, John Smith, 16/11/2016

12345678 56

34567822 67

12324654 98

你应该

File name: bbm.txt

Module code: BBM

Module Name: PHP

Tutor: John Smith

The award date: 16/11/2016

12345678: 56

34567822: 67

12324654: 98

我得到了

BBM PHP John Smith 16/11/2016

12345678 56

34567822 67

12324654 98

请,需要帮助。

function readLineByLine () {
    if (file_exists('bbm.txt')){
        $tmaFileToOpen = fopen("bbm.txt", "r"); // open our bbm.txt file
        if ($tmaFileToOpen) { // true if no errors occured 
            while(!feof($tmaFileToOpen)) {
                // variable $result will display data in line by line
                $result = fgets($tmaFileToOpen, 1024);
                $split = explode(',', $result);
                echo '<p>' . $split[0] . $split[1] . $split[2] . $split[3] . '</p>';
            }
        } else { // if an error occured message will be displayed
            echo '<p>There is an error openning file!</p>';
        }
        fclose($tmaFileToOpen); // Close our bbm.txt file 
    } else { // if file doesn't exist message will be displayed
        echo '<p>File requested not found!</p>';
    }
}
    echo readLineByLine();

1 个答案:

答案 0 :(得分:0)

以上评论是正确的:

'<p>Module Code:' . $split[0] .'<br>Module Name: '. $split[1] .'<br>Tutor'. $split[2] .'<br>The award date: '. $split[3] . '</p>';