在php中如何在一个数组中获取一个文件的多行
我的日志文件如下所示:在php中如何在一个数组中获取一个文件的多行,php,arrays,Php,Arrays,我的日志文件如下所示: --f15a0000-A-- [30/Aug/2018:14:06:33 +0200] W4fdyYHC0Xb8YDuIqk5YQgAAAD0 127.0.0.1 55454 127.0.0.1 80 --f15a0000-B-- GET /FormValidation/page1.php HTTP/1.1 Host: localhost Connection: keep-alive Upgrade-Insecure-Requests: 1 User-Agent: Moz
--f15a0000-A--
[30/Aug/2018:14:06:33 +0200] W4fdyYHC0Xb8YDuIqk5YQgAAAD0 127.0.0.1 55454 127.0.0.1 80
--f15a0000-B--
GET /FormValidation/page1.php HTTP/1.1
Host: localhost
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: http://localhost/FormValidation/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
tanuser: 00198343
--f15a0000-F--
HTTP/1.1 200 OK
X-Powered-By: PHP/5.6.35
Content-Length: 851
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8
--f15a0000-Z--
--bb410000-A--
[30/Aug/2018:14:06:37 +0200] W4fdzYHC0Xb8YDuIqk5YQwAAAD0 127.0.0.1 55454 127.0.0.1 80
--bb410000-B--
POST /FormValidation/validation.php HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 33
Accept: */*
Origin: http://localhost
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost/FormValidation/page1.php
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
tanuser: 00198343
--bb410000-C--
name1=test&email1=ssn%40gmail.com
--bb410000-F--
HTTP/1.1 200 OK
X-Powered-By: PHP/5.6.35
Content-Length: 17
Keep-Alive: timeout=5, max=98
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8
--bb410000-Z--
我想构建一个数组,每个位置包含一个块
例如:
数组[0]['rawdata']
包含从--f15a0000-A--到--f15a0000-Z--
的第一个块
数组[1]['rawdata']
包含--bb410000-A--to--bb410000-Z--
的第二个块
有人能帮我怎么做吗?这里似乎有一个模式,您可以使用正则表达式和
preg\u match\u all
。正则表达式,如
--([a-z\d]+)-A--[\s\S]+?--\1-Z--
似乎可以处理您的示例数据
--
是文本。[a-z\d]
是一个字符a
到z
,\d
是一个数字。+
允许它是这些字符中的一个或多个。()
捕获组。-A--
是一个文本搜索(A
似乎是一个起始块。)\s\s
是一个空白或非空白,也就是继续搜索+?
将查找匹配项,直到找到下一个字符串。我们搜索的下一个字符串与开始时的标识符相同,但结尾块有一个Z,
--\1-Z--
。\1
是对我们创建的第一个捕获组的反向引用
PHP用法:
请注意,PHP的使用还需要正则表达式上的起始和结束分隔符。通读该文件,每次在开头找到一个-,就可以启动或停止一个数组条目
$array = array();
$fh = fopen($file,"r");
$started = false;
while (!feof($fh)) {
$line = fgets($fh);
if($started) {
$temp .= $line;
if(strpos($line, "--") === 0 && strpos($line, "-Z-") > 0) {
$started = false;
$array[] = $temp;
}
}
if(strpos($line, "--") === 0 && strpos($line, "-A-") > 0) {
$started = true;
$temp = $line;
}
}
fclose($fh);
print_r($array);
我想这应该可以你试过什么?给我们看一些代码。打开文件,读它,当你看到你想要的标记时,把它存储在你的数组中。或者将整个文件读取一次,放入一个数组中,将不需要的内容grep出来。试试看,我们能帮上忙。实际上我不知道怎么开始。我不想要任何代码。如果你能解释一下我能做什么,那将非常有帮助。非常感谢Open,read。。。功能:或读取整个文件:
文件\u获取内容
或文件
。另一个可能值得注意的是,您需要0
索引的$matches
。1
索引是第一个捕获组,这在搜索后并不重要<代码>打印($matches[0])密码>很棒的答案。请始终发布完整的、深思熟虑的、有教育意义的答案。我们需要更多像你这样的海报!很好。
$array = array();
$fh = fopen($file,"r");
$started = false;
while (!feof($fh)) {
$line = fgets($fh);
if($started) {
$temp .= $line;
if(strpos($line, "--") === 0 && strpos($line, "-Z-") > 0) {
$started = false;
$array[] = $temp;
}
}
if(strpos($line, "--") === 0 && strpos($line, "-A-") > 0) {
$started = true;
$temp = $line;
}
}
fclose($fh);
print_r($array);