2 回答
TA贡献1801条经验 获得超8个赞
你应该做的是:
打开文件
逐行读取
用分隔符空格分解行(“”){将字符串转换为数组}
获取数组的第三个元素(索引 2)
例子
$fn = fopen("log.txt","r");
while(! feof($fn)) {
$line = fgets($fn);
$val = explode(" ", $line)[2];
echo $val."<br>";
}
fclose($fn);
TA贡献1797条经验 获得超6个赞
嘿使用正则表达式来解决它。
首先,使用file_get_contents()
PHP的函数读取文件。该函数将整个文件读入一个字符串。 将整个文件读入一个字符串 - PHP
现在使用 PHPpreg_match_all()
函数,从字符串中获取您想要的任何内容。 PHP: preg_match_all - 手册
看:
/**
$file = "2019-05-14 00:00:00 1558449 v7.41 33309
2019-05-14 00:00:00 1335564 v7.38 33600
2019-05-14 00:00:00 1584141 v7.34 39800
2019-05-14 00:00:00 1556654 v7.38 33600
2019-05-14 00:00:00 1456993 v7.39 33600
2019-05-14 00:00:00 1581138 v7.39 33600
2019-05-14 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 33600
2019-05-14 00:00:00 1553292 v7.35 33309";
*/
$file = file($addressOfFile);
preg_match_all("/.*([0-9]{7}).*/", $file, $matches);
其结果将是:
array(2) {
[0] =>
array(10) {
[0] =>
string(39) "2019-05-14 00:00:00 1558449 v7.41 33309"
[1] =>
string(39) "2019-05-14 00:00:00 1335564 v7.38 33600"
[2] =>
string(39) "2019-05-14 00:00:00 1584141 v7.34 39800"
[3] =>
string(39) "2019-05-14 00:00:00 1556654 v7.38 33600"
[4] =>
string(39) "2019-05-14 00:00:00 1456993 v7.39 33600"
[5] =>
string(39) "2019-05-14 00:00:00 1581138 v7.39 33600"
[6] =>
string(39) "2019-05-14 00:00:00 1224055 v7.39 33600"
[7] =>
string(39) "2019-05-14 00:00:00 1557900 v7.35 33309"
[8] =>
string(39) "2019-05-14 00:00:00 1576229 v7.39 33600"
[9] =>
string(39) "2019-05-14 00:00:00 1553292 v7.35 33309"
}
[1] =>
array(10) {
[0] =>
string(7) "1558449"
[1] =>
string(7) "1335564"
[2] =>
string(7) "1584141"
[3] =>
string(7) "1556654"
[4] =>
string(7) "1456993"
[5] =>
string(7) "1581138"
[6] =>
string(7) "1224055"
[7] =>
string(7) "1557900"
[8] =>
string(7) "1576229"
[9] =>
string(7) "1553292"
}
}
现在您可以通过$matches[1]数组访问您想要的内容。
- 2 回答
- 0 关注
- 121 浏览
添加回答
举报