Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何读取php中的最后一行并解析出某些列_Php - Fatal编程技术网

如何读取php中的最后一行并解析出某些列

如何读取php中的最后一行并解析出某些列,php,Php,我对php非常陌生。我真的很感激这里所有的帮助 我正在使用sftp登录到服务器并获取文件。我只对文件中的最后一行感兴趣。最后一行由制表符分隔。我想将15、16、19、20和21列值存储到5个不同的变量中。最后一行是这样的: 7 1 0 59422170 306669 20188 20386 0 0 39787 59981 2014 67796 48953 2 7 90 1 1.81 11.3 12:19:50 当我发出这个cCurl命令获取文

我对php非常陌生。我真的很感激这里所有的帮助

我正在使用sftp登录到服务器并获取文件。我只对文件中的最后一行感兴趣。最后一行由制表符分隔。我想将15、16、19、20和21列值存储到5个不同的变量中。最后一行是这样的:

7   1   0   59422170     306669 20188 20386     0     0 39787  59981  2014  67796 48953  2  7 90  1  1.81  11.3 12:19:50
当我发出这个cCurl命令获取文件时,我如何读取文件的最后一行并解析出最后一行中的某些列

<?php
$user="user";
$pass="pass";
$c = curl_init("sftp://$user:$pass@server1/vmstat");
curl_setopt($c, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($c);
curl_close($c);
#echo $data;

?>

为什么要使用php

使用输入文件:

7   1   0   59422170     306669 20188 20386     0     0 39787  59981  2014  67796 48953  2  7 90  1  1.81  11.3 12:19:50
和命令行:

tail -n1 myfile | sed 's/\s\s*/ /g' | cut -d' ' -f15,16,19,20,21
结果是:

2 7 1.81 11.3 12:19:50
最后一行 如果文件很小,但不久前写过,可能会有点过头。返回文件/流的最后n行

function tail($file, $lines = 10, $buffer = 4096)
{
        if(is_resource($file) && (get_resource_type($file) == 'file' || get_resource_type($file) == 'stream'))
                $f = $file;
        elseif(is_string($file))
                $f = fopen($file, 'rb');
        else
                throw new Exception('$file must be either a resource (file or stream) or a filename.');

        $output = ''; 
        $chunk = '';

        fseek($f, -1, SEEK_END);

        if(fread($f, 1) != "\n")
                $lines -= 1;

        while(ftell($f) > 0 && $lines >= 0)
        {
                $seek = min(ftell($f), $buffer);
                fseek($f, -$seek, SEEK_CUR);

                $output = ($chunk = fread($f, $seek)).$output;
                fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
                $lines -= substr_count($chunk, "\n");
        }

        while($lines++ < 0)
                $output = substr($output, strpos($output, "\n")+1);

        fclose($f); 
        return $output; 
}

什么是
15、16、19、20和21列
;只需使用
\t
作为分隔符。@Oswalt,谢谢你。但是当我回音到$last_行时,我什么也看不到。最后一行是空行吗?还是
end
给你
false
?@Frits van Campen,结尾似乎是空的,你会怎么做-1Try
end($data)$第二行到最后一行=上一行($data)
您可能需要另一个
prev
,我不确定。
如果($last_line==false){echo'在$data'中没有“last line”}
我需要使用php从远程服务器获取文件。
function tail($file, $lines = 10, $buffer = 4096)
{
        if(is_resource($file) && (get_resource_type($file) == 'file' || get_resource_type($file) == 'stream'))
                $f = $file;
        elseif(is_string($file))
                $f = fopen($file, 'rb');
        else
                throw new Exception('$file must be either a resource (file or stream) or a filename.');

        $output = ''; 
        $chunk = '';

        fseek($f, -1, SEEK_END);

        if(fread($f, 1) != "\n")
                $lines -= 1;

        while(ftell($f) > 0 && $lines >= 0)
        {
                $seek = min(ftell($f), $buffer);
                fseek($f, -$seek, SEEK_CUR);

                $output = ($chunk = fread($f, $seek)).$output;
                fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
                $lines -= substr_count($chunk, "\n");
        }

        while($lines++ < 0)
                $output = substr($output, strpos($output, "\n")+1);

        fclose($f); 
        return $output; 
}
$columns = explode("\t",$line);

$foo = $columns[15];
...