PHP - 从文本文件的末尾阅读 [英] PHP - reading from the end of a text file

查看:122
本文介绍了PHP - 从文本文件的末尾阅读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个逗号分隔值的文本文件。

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

例如:

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

现在,我仅需要读取数据的最后的设置,即只从 12994917389996,12,13,14,15,等等...

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

在这种情况下,我想 12994917389996 大于130和其他值都小于130。所以,我需要读取 12994917389996 来文件的末尾...

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...

希望你得到它!

编辑1:不,这是它不断得到更新一个动态文本文件,我需要每次只读最后一组数据

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 !

编辑2:我的code

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);
?>  

在这里,我用$ KEYVAL [X] []至[]可存储交替值巨人XXXXXXXXXXXXX价值观和KEYVAL [Y]和KEYVAL [Y1] []存储之后,GIANT(XXXXXXXXXXXXX)值....这应该澄清一切!

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 !

推荐答案

对不起,我不知道我知道了......你想从除了第一个第一个数字130多家大让所有的数字?男人,这是复杂的:D

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

这个怎么样?

$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));

输出:

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

这篇关于PHP - 从文本文件的末尾阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆