开发者

Read Line From Text File

I have been given a textfile which contains data on time sheet. That line contains a name and a department and their time out/in.The code below allows me to display the entire textfile but I want to read in the values by name and department from file and wa开发者_如何学编程nt to display that .

<?php
$file = fopen("C:\Bhrugisha mam.txt","r") or exit("Unable to open file!");
while(!feof($file))  
{
echo fgets($file). "<br />";
}
fclose($file);
?>

this is some data from my text file...

                                                               VCANTECH - Log Report


                 6                 Bhrugisha Shah
                 Hr
                 Department:         HR
                 August, 2011                                                                                           Page 1 of 2
                 -----------------------------------------------------------------------------------------------------------------
                 Date :    01/Aug/2011         Shift : 1       (     9:30:00 AM     To      6:30:00 PM     )
                                     LOG TIME :    9:23:00 AM                 LOG TYPE :    IN
                                     LOG TIME :    1:55:00 PM                 LOG TYPE :    OUT
                                     LOG TIME :    2:18:00 PM                 LOG TYPE :    IN
                                     LOG TIME :    6:45:00 PM                 LOG TYPE :    OUT


This should get you there

$input = fread( $file, filesize("PATHTOFILE"));

preg_match("%Department:[\s]{1,}[a-z]{1,}%i", $input, $department);

var_dump($department);

//OUTPUT array(1) { [0]=> array(1) { [0]=> string(22) "Department: HR" }

preg_match_all("%log time :    [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2} (AM|PM)                 LOG TYPE :    (IN|OUT)%i", $input, $matches);

var_dump($matches);

/*
 OUTPUT 
 array(3) { [0]=> array(4) { [0]=> string(57) "LOG TIME : 9:23:00 AM LOG TYPE : IN" [1]=> string(58) "LOG TIME : 1:55:00 PM LOG TYPE : OUT" [2]=> string(57) "LOG TIME : 2:18:00 PM LOG TYPE : IN" [3]=> string(58) "LOG TIME : 6:45:00 PM LOG TYPE : OUT" } [1]=> array(4) { [0]=> string(2) "AM" [1]=> string(2) "PM" [2]=> string(2) "PM" [3]=> string(2) "PM" } [2]=> array(4) { [0]=> string(2) "IN" [1]=> string(3) "OUT" [2]=> string(2) "IN" [3]=> string(3) "OUT" } }
*/

foreach($matches[0] as $row){

    preg_match("%[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2} (AM|PM)%", $row, $logTime);
    //var_dump($logTime);
    //OUTPUT array(2) { [0]=> string(10) "9:23:00 AM" [1]=> string(2) "AM" } array(2) { [0]=> string(10) "1:55:00 PM" [1]=> string(2) "PM" } array(2) { [0]=> string(10) "2:18:00 PM" [1]=> string(2) "PM" } array(2) { [0]=> string(10) "6:45:00 PM" [1]=> string(2) "PM" }
    $logTime = $logTime[0];

    preg_match("%(IN|OUT)%",$row, $inOut);
    var_dump($inOut);
    //OUTPUT array(2) { [0]=> string(2) "IN" [1]=> string(2) "IN" } array(2) { [0]=> string(3) "OUT" [1]=> string(3) "OUT" } array(2) { [0]=> string(2) "IN" [1]=> string(2) "IN" } array(2) { [0]=> string(3) "OUT" [1]=> string(3) "OUT" }
    $inOut = $inOut[0];

}

I would recommend making some edits to the regex's, as I just copy and pasted the spaces. [\s]{1,} can be used in place of the crazy spaces :P


You can use

$file = file('filename.txt');

It will return the array with each element containing each line.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜