繁体   English   中英

从php中的.txt文件中获取特定文本

[英]get specific text from .txt file in php

我有一个这样的日志文件:

2013-04-08-03-17-52: Cleaning Up all data for restart operation
2013-04-08-03-18-02: Creating new instance of app before closing
2013-04-08-03-18-03: New instance created and running
2013-04-08-03-18-03: Application started

目前,我每秒加载完整的日志文件,使用jquery ajax向用户显示,因为这样效率太低,我试图想办法从日志文件中加载只更新的行。

他们是否只能在特定的时间戳之后获取线路2013-04-08-03-18-03 ,为此我将管理一个带有上一个时间戳的变量,并且每次我获得新线路时都会更新它。

我是Php的新手,只知道阅读和编写文件的基础知识。

您可能希望首先检查文件修改时间,以查看是否需要重新加载日志文件。 您可以使用filemtime

此外,您可以使用file_get_contents -function使用偏移量从某个点读取文件。

编辑 :那么,它是如何工作的?

假设您已将最后修改时间存储在会话变量$_SESSION['log_lastmod'] ,并将最近的偏移量存储在$_SESSION['log_offset']

session_start();

// First, check if the variables exist. If not, create them so that the entire log is read.
if (!isset($_SESSION['log_lastmod']) && !isset($_SESSION['log_offset'])) {
    $_SESSION['log_lastmod'] = 0;
    $_SESSION['log_offset'] = 0;
}

if (filemtime('log.txt') > $_SESSION['log_lastmod']) {
    // read file from offset
    $newcontent = file_get_contents('log.txt', NULL, NULL, $_SESSION['log_offset']);
    // set new offset (add newly read characters to last offset)
    $_SESSION['log_offset'] += strlen($newcontent);
    // set new lastmod-time
    $_SESSION['log_lastmod'] = filemtime('log.txt');

    // manipulate $newcontent here to what you want it to show
} else {
    // put whatever should be returned to the client if there are no updates here
}

你可以试试

session_start();
if (! isset($_SESSION['log'])) {
    $log = new stdClass();
    $log->timestamp = "2013-04-08-03-18-03";
    $log->position = 0;
    $log->max = 2;
    $_SESSION['log'] = &$log;
} else {
    $log = &$_SESSION['log'];
}

$format = "Y-m-d-:g:i:s";
$filename = "log.txt";

// Get last date
$dateLast = DateTime::createFromFormat($format, $log->timestamp);

$fp = fopen($filename, "r");
fseek($fp, $log->position); // prevent loading all file to memory

$output = array();
$i = 0;
while ( $i < $log->max && ! feof($fp) ) {
    $content = fgets($fp);

    // Check if date is current
    if (DateTime::createFromFormat($format, $log->timestamp) < $dateLast)
        continue;

    $log->position = ftell($fp); // save current position
    $log->timestamp = strstr($content, ":", true); // save curren time;
    $output[] = $content;
    $i ++;
}

fclose($fp);
echo json_encode($output); // send to ajax 

尝试这样的事情:

<?php
    session_start();
    $lines = file(/*Insert logfile path here*/);
    if(isset($_SESSION["last_date_stamp"])){
        $new_lines = array();
        foreach($lines as $line){
            // If the date stamp is newer than the session variable, add it to $new_lines
        }
        $returnLines = array_reverse($newLines); // the reverse is to put the lines in the right order
    } else {
        $returnLines = $lines;
    }
    $_SESSION['last_date_stamp'] = /* Insert the most recent date stamp here */;
    // return the $returnLines variable
?>

这样做是逐行读取文件,如果会话变量已经存在,则将日期戳比会话变量中的行更新的所有行添加到返回变量$returnLines ,如果不存在会话变量的话将所有行放在$returnLines中的文件中。

在此之后,create.updates会话变量以供下次执行代码时使用。 最后,它使用$returnLines变量返回日志文件数据。

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM