Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/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_Search - Fatal编程技术网

使用PHP从输入模式搜索日志文件

使用PHP从输入模式搜索日志文件,php,search,Php,Search,我的问题是创建一个可以在日志文件中搜索的搜索函数 模式是这样的: node-id > command -Date-other descriptions Output starts Output ends node-id > another command 现在,我的问题是从一个日志文件中剪切一个命令的输出,并将其与另一个日志文件的输出进行比较。我无法找到搜索内容并将其存储到数组或文件中以供进一步比较的方法。您不能从文件中剪切内容,只能更改文件并重新保存(根据文件系统的不同,您可能会

我的问题是创建一个可以在日志文件中搜索的搜索函数

模式是这样的:

node-id > command
-Date-other descriptions
Output starts
Output ends
node-id > another command

现在,我的问题是从一个日志文件中剪切一个命令的输出,并将其与另一个日志文件的输出进行比较。我无法找到搜索内容并将其存储到数组或文件中以供进一步比较的方法。

您不能从文件中剪切内容,只能更改文件并重新保存(根据文件系统的不同,您可能会直接处理相同的内容,但在这里问这个问题意味着您没有这样低级的文件系统经验)

在php中,它类似于:

$data = file_get_contents($fileToCutFrom);
// ... modify data here, e.g. if it's xml you might wanna use SimpleXML or DOMDocument or some other xml parser, like ganon ( https://code.google.com/p/ganon/ ) - the later one is pretty good if you know you might have incomplete/invalid xml
file_put_contents($fileToCutFrom, $modifiedData);


// and now append to the other file
file_put_contents($fileToAppendTo, $dataToAppend, FILE_APPEND);
但是要知道,如果从多个文件执行此操作,可能会遇到严重的争用情况,因为文件的get内容不是原子的,file的put内容也不是原子的,而且还存在要执行多行的问题。因此,如果代码可能一次执行多次,则可能需要使用单个脚本实例它执行所有的“剪切”和附加操作,打开一个套接字,其他脚本只向该文件发送它们需要的内容

另外,如果你想在新数据进来时继续这样做,你可能想看看inotify(linux)或你系统上的同等产品(osx也有一个文件通知服务,但我不知道windows)


如果您需要一些不同的内容,您可能希望在问题中添加信息。

您迄今为止尝试了哪些内容?您尝试了哪些内容后遇到了哪些错误或问题?请提供一些代码并说明您解决此问题的想法。