Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.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中的.txt文件中获取特定文本_Php_String_Text - Fatal编程技术网

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

从php中的.txt文件中获取特定文本,php,string,text,Php,String,Text,我有一个这样的日志文件: 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 目前,我正在每秒加载一次

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

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
目前,我正在每秒加载一次完整的日志文件,以便使用jqueryajax向用户显示它,因为这样做效率太低,所以我试图找出一些方法,仅从日志文件加载更新的行

他们有没有办法只在特定的时间戳之后才能获取行数
2013-04-08-03-18-03
为此,我将管理一个带有最后一个时间戳的变量,并将在每次收到新行时更新它


我对Php有点陌生,只知道读写文件的基本知识。

您可能需要先检查文件修改时间,看看是否需要重新加载日志文件。您可以使用-函数来实现这一点

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

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

假设您已将上次修改时间存储在会话变量
$\u session['log\u lastmod']
中,并将最近的偏移量存储在
$\u session['log\u 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
}

试着这样做:

<?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
变量返回日志文件数据

希望这有帮助。

你可以试试

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 
session_start();
如果(!isset($\u会话['log'])){
$log=新的stdClass();
$log->timestamp=“2013-04-08-03-18-03”;
$log->position=0;
$log->max=2;
$_会话['log']=&$log;
}否则{
$log=&$_会话['log'];
}
$format=“Y-m-d-:g:i:s”;
$filename=“log.txt”;
//最后一次约会
$dateLast=DateTime::createFromFormat($format,$log->timestamp);
$fp=fopen($filename,“r”);
fseek($fp,$log->position);//防止将所有文件加载到内存
$output=array();
$i=0;
而($i<$log->max&&!feof($fp)){
$content=fgets($fp);
//检查日期是否为当前日期
if(DateTime::createFromFormat($format,$log->timestamp)<$dateLast)
继续;
$log->position=ftell($fp);//保存当前位置
$log->timestamp=strstrstr($content,:“,true);//保存当前时间;
$output[]=$content;
$i++;
}
fclose($fp);
echo json_encode($output);//发送到ajax

为什么不直接加载尾部条目?@Baba你能解释一下吗。。我不知道它是怎么工作的请看下面的代码。。。引入最大行数还能够将文件指针移动到上次重试的起始位置,并进行日期验证如何在不知道新行起始位置的情况下设置偏移量使用-函数记录以前访问的文件的大小以获取偏移量。一旦阅读,保存新的文件大小。这看起来不错,你能用一些代码解释一下吗…我对php有点陌生(
;)。。工作。。。!谢谢你的代码。我想他的问题是将文件发送回jQuery,而不仅仅是新的行。我尝试了你的代码。。但是似乎没有返回新行。相反,它只返回两个随机行…IDK but
$log->timestamp=strstrstr($content,“:”,true)在此。。如果时间戳后的文本中有多个
,该怎么办?