猿问

从日志文件中读取选定的列

你能帮我解决这个问题吗?我有一个包含列的日志文件。我只想读一个专栏。


这是示例日志文件:


2019-05-14 00:00:00 1558449 v7.41 33309

2019-05-14 00:00:00 1335564 v7.38 33600

2019-05-14 00:

059-04-0404-04-04040 00:00:00 1556654 v7.38 33600

2019-05-14 00:00:00 1456993 v7.39 33600

2019-05-14 00:00:00 15811938-0316:038-0316:00:00:

00 1224055 v7.39 33600

2019-05-14 00:00:00 1557900 v7.35 33309

2019-05-14 00:00:00 1576229 v7.39

3015-3009:00:00 3015-3009


我只需要阅读这个 1558449 专栏。


我试过的代码在这个链接中:


https://www.quora.com/How-do-I-to-read-lines-from-text-file-into-a-table-format-in-PHP


我期待输出将是这样的:


1558449

1335564

1584141

1556654

1456993

1581138

1224055

1557900

1576229

1553292


隔江千里
浏览 125回答 2
2回答

蝴蝶刀刀

你应该做的是:打开文件逐行读取用分隔符空格分解行(“”){将字符串转换为数组}获取数组的第三个元素(索引 2)例子$fn = fopen("log.txt","r");while(! feof($fn))&nbsp; {&nbsp; &nbsp; $line = fgets($fn);&nbsp; &nbsp; $val = explode(" ", $line)[2];&nbsp; &nbsp; echo $val."<br>";}fclose($fn);

FFIVE

嘿使用正则表达式来解决它。首先,使用file_get_contents()PHP的函数读取文件。该函数将整个文件读入一个字符串。&nbsp;将整个文件读入一个字符串 - PHP现在使用 PHPpreg_match_all()函数,从字符串中获取您想要的任何内容。&nbsp;PHP: preg_match_all - 手册看:/**$file = "2019-05-14 00:00:00 1558449 v7.41 333092019-05-14 00:00:00 1335564 v7.38 336002019-05-14 00:00:00 1584141 v7.34 398002019-05-14 00:00:00 1556654 v7.38 336002019-05-14 00:00:00 1456993 v7.39 336002019-05-14 00:00:00 1581138 v7.39 336002019-05-14 00:00:00 1224055 v7.39 336002019-05-14 00:00:00 1557900 v7.35 333092019-05-14 00:00:00 1576229 v7.39 336002019-05-14 00:00:00 1553292 v7.35 33309";*/$file = file($addressOfFile);preg_match_all("/.*([0-9]{7}).*/", $file, $matches);其结果将是:array(2) {&nbsp; [0] =>&nbsp; array(10) {&nbsp; &nbsp; [0] =>&nbsp; &nbsp; string(39) "2019-05-14 00:00:00 1558449 v7.41 33309"&nbsp; &nbsp; [1] =>&nbsp; &nbsp; string(39) "2019-05-14 00:00:00 1335564 v7.38 33600"&nbsp; &nbsp; [2] =>&nbsp; &nbsp; string(39) "2019-05-14 00:00:00 1584141 v7.34 39800"&nbsp; &nbsp; [3] =>&nbsp; &nbsp; string(39) "2019-05-14 00:00:00 1556654 v7.38 33600"&nbsp; &nbsp; [4] =>&nbsp; &nbsp; string(39) "2019-05-14 00:00:00 1456993 v7.39 33600"&nbsp; &nbsp; [5] =>&nbsp; &nbsp; string(39) "2019-05-14 00:00:00 1581138 v7.39 33600"&nbsp; &nbsp; [6] =>&nbsp; &nbsp; string(39) "2019-05-14 00:00:00 1224055 v7.39 33600"&nbsp; &nbsp; [7] =>&nbsp; &nbsp; string(39) "2019-05-14 00:00:00 1557900 v7.35 33309"&nbsp; &nbsp; [8] =>&nbsp; &nbsp; string(39) "2019-05-14 00:00:00 1576229 v7.39 33600"&nbsp; &nbsp; [9] =>&nbsp; &nbsp; string(39) "2019-05-14 00:00:00 1553292 v7.35 33309"&nbsp; }&nbsp; [1] =>&nbsp; array(10) {&nbsp; &nbsp; [0] =>&nbsp; &nbsp; string(7) "1558449"&nbsp; &nbsp; [1] =>&nbsp; &nbsp; string(7) "1335564"&nbsp; &nbsp; [2] =>&nbsp; &nbsp; string(7) "1584141"&nbsp; &nbsp; [3] =>&nbsp; &nbsp; string(7) "1556654"&nbsp; &nbsp; [4] =>&nbsp; &nbsp; string(7) "1456993"&nbsp; &nbsp; [5] =>&nbsp; &nbsp; string(7) "1581138"&nbsp; &nbsp; [6] =>&nbsp; &nbsp; string(7) "1224055"&nbsp; &nbsp; [7] =>&nbsp; &nbsp; string(7) "1557900"&nbsp; &nbsp; [8] =>&nbsp; &nbsp; string(7) "1576229"&nbsp; &nbsp; [9] =>&nbsp; &nbsp; string(7) "1553292"&nbsp; }}现在您可以通过$matches[1]数组访问您想要的内容。
随时随地看视频慕课网APP
我要回答