开发者

PHP - reading from the end of a text file

I have a text file with values separated by a comma.

Example:

1299491735618,10,84,10,121.6,10,120.0,12994917389996,12, 13, 14, 15, and so on ..

Now, I need to read only the last "set" of data, i.e. only from 12994917389996,12, 13, 14, 15, and so on ...

In this case, I guess 12994917389996 is greater than 130 and other values are less than 130. So, I need to read FROM 12994917389996 to the end of file...

Hope you got it!

EDIT 1: No, It's a dynamic text开发者_开发知识库 file which keeps getting updated and I need to read only the last set of data every time !

EDIT 2 : MY code

<?php
$file = fopen('graph.txt', 'r') or die("can't open file");

if ($file) {
    while (!feof($file)) {
        $line = trim(fgets($file));
        if (strlen($line)) {
            $fields = explode(",", $line);
            $num = count($fields);
            for ($i = 0; $i < $num; $i++) {
                $keyval[$i] = $fields[$i];
                if ($i == 0) {
                    $keyval['x'][] = $fields[$i];
                    echo $keyval['x'][0];
                }
                else {
                    if ($i % 2 == 0) $keyval['y'][] = $fields[$i];
                    else $keyval['y1'][] = $fields[$i];
                }
            }
        }
    }
}

fclose($file);
?>  

HERE , I use $keyval[x][] to store the giant xxxxxxxxxxxxx values and keyval[y][] and keyval[y1][] to store ALTERNATING values AFTER That GIANT (xxxxxxxxxxxxx ) value .... This should clarify everything !


Sorry, I'm not sure I get it... You want to get all numbers from the first number bigger than 130 except the first one? Man, that's complicated :D

How about this?

$str = "1299491735618,10,84,10,121.6,10,120.0,12994917389996,12,13,14,15,16,17";

$arr = explode(',', $str);

$i = 1;
while ($i < count($arr) && floatval($arr[$i]) < 130)
  $i++;

print_r(array_slice($arr, $i));

Output:

Array
(
    [0] => 12994917389996
    [1] => 12
    [2] => 13
    [3] => 14
    [4] => 15
    [5] => 16
    [6] => 17
)


Hope this would help.

$ echo '1299491735618,10,84,10,121.6,10,120.0,12994917389996,12, 13, 14, 15'  > file
$ php -a
php > $fp = fopen('/path/to/file', 'r');
php > fseek($fp, 38);
php > echo fgets($fp);
12994917389996,12, 13, 14, 15

If not, could you be more specific about data structure stored in file?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜