PHP提取文本内容到对应表格里
本帖最后由 zoenbo 于 2020-11-28 15:40 编辑如何把这个TXT里的内容,只提取time,ip,user_agent,url,query相应的值,里边的”\/“替换成”/“
3.txt内容
{"time":"2020-11-27 09:41:01","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":""}
{"time":"2020-11-27 09:41:43","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":""}
{"time":"2020-11-27 09:44:16","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":""}
{"time":"2020-11-27 10:03:01","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":"null=1"}
{"time":"2020-11-27 10:03:29","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":""}
<?php
$str= file_get_contents("3.txt");//将文件中的内容读成字符串
$str1=explode("\n",$str);//按换行进行拆分//单引号是转义字符,所以必须使用双引号
$i=0;
foreach($str1 as $value){
if($value=="")continue; //最后一行是空格,当找出最后一行的时候直接跳过,不进行下面的操作
//此时对此一组数据进行拆分
$cols=explode("|",$value);//按竖线进行拆分
$cols=explode('","',$value);//按竖线进行拆分
$data[$i]=$cols; //将每次得到的存放在一个数组中
$i++;
}
$count = count($data);
$page = $_GET['page'];
$limit = $_GET['limit'];
$firstIndex=($page-1)*$limit;
$new = array_slice($data,$firstIndex,$limit);
$data1 = [];
$i = 0;
foreach ($new as $v){
$data[$i]['time'] = $v;
$data[$i]['ip'] = $v;
$data[$i]['user_agent'] = $v;
$data[$i]['url'] = $v;
$data[$i]['query'] = $v;
$i++;
}
$response_list=[
'code'=>'0',
'msg'=>'',
'count'=>$count,
'data'=>$data
];
echo json_encode($response_list);
?> 本帖最后由 lzh68575536 于 2020-11-28 15:12 编辑
$str = '{"time":"2020-11-27 09:41:01","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":""}
{"time":"2020-11-27 09:41:43","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":""}
{"time":"2020-11-27 09:44:16","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":""}
{"time":"2020-11-27 10:03:01","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":"null=1"}
{"time":"2020-11-27 10:03:29","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":""}';
$str1=explode("\n",$str);
$data = [];
foreach($str1 as $value){
$val = json_decode($value,true);
$data[] = [
'time'=>$val['time'],
'ip'=>$val['ip'],
'user_agent'=>$val['user_agent'],
'url'=>$val['url'],
'query'=>$val['query'],
];
}
echo '<pre>';
var_dump($data);
echo '</pre>';
显示结果:
array(5) {
=>
array(5) {
["time"]=>
string(19) "2020-11-27 09:41:01"
["ip"]=>
string(11) "192.168.0.1"
["user_agent"]=>
string(131) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36 Edg/86.0.622.69"
["url"]=>
string(16) "http://damn.com/"
["query"]=>
string(0) ""
}
=>
array(5) {
["time"]=>
string(19) "2020-11-27 09:41:43"
["ip"]=>
string(11) "192.168.0.1"
["user_agent"]=>
string(131) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36 Edg/86.0.622.69"
["url"]=>
string(16) "http://damn.com/"
["query"]=>
string(0) ""
}
=>
array(5) {
["time"]=>
string(19) "2020-11-27 09:44:16"
["ip"]=>
string(11) "192.168.0.1"
["user_agent"]=>
string(131) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36 Edg/86.0.622.69"
["url"]=>
string(16) "http://damn.com/"
["query"]=>
string(0) ""
}
=>
array(5) {
["time"]=>
string(19) "2020-11-27 10:03:01"
["ip"]=>
string(11) "192.168.0.1"
["user_agent"]=>
string(131) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36 Edg/86.0.622.69"
["url"]=>
string(16) "http://damn.com/"
["query"]=>
string(6) "null=1"
}
=>
array(5) {
["time"]=>
string(19) "2020-11-27 10:03:29"
["ip"]=>
string(11) "192.168.0.1"
["user_agent"]=>
string(131) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36 Edg/86.0.622.69"
["url"]=>
string(16) "http://damn.com/"
["query"]=>
string(0) ""
}
} echojson_encode(str_replace("\/","/",$response_list));
试试
后面的应该就不用说了吧....好不容看到个问PHP的{:1_918:} 不知道你的想法 直接转数组不就好了吗
<?php
$str = '{"time":"2020-11-27 09:41:01","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":""}
{"time":"2020-11-27 09:41:43","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":""}
{"time":"2020-11-27 09:44:16","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":""}
{"time":"2020-11-27 10:03:01","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":"null=1"}
{"time":"2020-11-27 10:03:29","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":""}';
$str1=explode("\n",$str);
foreach ($str1 as $k => $v) {
$array[$k] = json_decode($v,true);
}
var_dump(json_encode($array));
wginui 发表于 2020-11-28 15:03
echojson_encode(str_replace("\/","/",$response_list));
试试
不行的,大佬你帮我试试看哪有问题吧
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>统计数据</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/layui-src@2.5.5/src/css/layui.css">
</head>
<body>
<tableid="demo" lay-filter="test"></table>
<script src="https://cdn.jsdelivr.net/npm/layui-src@2.5.5/dist/layui.min.js"></script>
<script>
layui.use('table', function(){
var table = layui.table;
//第一个实例
table.render({
elem: '#demo'
,url: 'index2.php' //数据接口
,page: true //开启分页index
,cols: [[ //表头
{field: 'time', title: '时间', width:120, fixed: 'left'}
,{field: 'ip', title: 'IP', width:200}
,{field: 'user_agent', title: '用户代理', width:200}
,{field: 'url', title: '网址', width:200}
,{field: 'query', title: '参数', width: 300}
]]
});
});
</script>
</body>
</html> 第一次贴代码 不好意思
<?php
$str = '{"time":"2020-11-27 09:41:01","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":""}
{"time":"2020-11-27 09:41:43","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":""}
{"time":"2020-11-27 09:44:16","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":""}
{"time":"2020-11-27 10:03:01","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":"null=1"}
{"time":"2020-11-27 10:03:29","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":""}';
$str1=explode("\n",$str);
foreach ($str1 as $k => $v) {
$array[$k] = json_decode($v,true);
}
var_dump(json_encode($array));
$str = '{"time":"2020-11-27 09:41:01","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":""}
{"time":"2020-11-27 09:41:43","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":""}
{"time":"2020-11-27 09:44:16","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":""}
{"time":"2020-11-27 10:03:01","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":"null=1"}
{"time":"2020-11-27 10:03:29","ip":"192.168.0.1","user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/86.0.4240.198 Safari\/537.36 Edg\/86.0.622.69","url":"http:\/\/damn.com\/","query":""}';
$str1=explode("\n",$str);
foreach ($str1 as $k => $v) {
$array[$k] = json_decode($v,true);
}
var_dump(json_encode($array));
lzh68575536 发表于 2020-11-28 15:07
$str = '{"time":"2020-11-27 09:41:01","ip":"192.168.0.1","user_agent":"Moz ...
实际文件得有几百上千条,这么多有循环的办法没,我不会写 zoenbo 发表于 2020-11-28 15:12
实际文件得有几百上千条,这么多有循环的办法没,我不会写
只要读取文件就行了呗.不用写成$str就用你原来的
$str= file_get_contents("3.txt");//将文件中的内容读成字符串
$str1=explode("\n",$str);//按换行进行拆分//单引号是转义字符,所以必须使用双引号
即可,如果数据多了的话,可以分段读取
页:
[1]
2