吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1419|回复: 19
收起左侧

[已解决] PHP提取文本内容到对应表格里

[复制链接]
zoenbo 发表于 2020-11-28 14:19
本帖最后由 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] 纯文本查看 复制代码
<?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[0];
    $data[$i]['ip'] = $v[1];
    $data[$i]['user_agent'] = $v[2];
    $data[$i]['url'] = $v[3];
    $data[$i]['query'] = $v[4];
    $i++;
}

$response_list=[
    'code'=>'0',
    'msg'=>'',
    'count'=>$count,
    'data'=>$data
];
echo json_encode($response_list);

?>
微信截图_20201128141415.png

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

lzh68575536 发表于 2020-11-28 15:07
本帖最后由 lzh68575536 于 2020-11-28 15:12 编辑

[Asm] 纯文本查看 复制代码
    $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) {
  [0]=>
  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) ""
  }
  [1]=>
  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) ""
  }
  [2]=>
  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) ""
  }
  [3]=>
  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"
  }
  [4]=>
  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) ""
  }
}

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
zoenbo + 1 + 1 谢谢@Thanks!

查看全部评分

wginui 发表于 2020-11-28 15:03
echo  json_encode(str_replace("\/","/",$response_list));
试试
lzh68575536 发表于 2020-11-28 15:08
后面的应该就不用说了吧....好不容看到个问PHP的
章鱼27丶 发表于 2020-11-28 15:09
不知道你的想法 直接转数组不就好了吗
[PHP] 纯文本查看 复制代码
<?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));
 楼主| zoenbo 发表于 2020-11-28 15:09
wginui 发表于 2020-11-28 15:03
echo  json_encode(str_replace("\/","/",$response_list));
试试

不行的,大佬你帮我试试看哪有问题吧
[HTML] 纯文本查看 复制代码
<!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>
<table  id="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>
章鱼27丶 发表于 2020-11-28 15:09
第一次贴代码 不好意思
[PHP] 纯文本查看 复制代码
<?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));
章鱼27丶 发表于 2020-11-28 15:10
[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));
 楼主| zoenbo 发表于 2020-11-28 15:12
lzh68575536 发表于 2020-11-28 15:07
[mw_shl_code=php,true]    $str = '{"time":"2020-11-27 09:41:01","ip":"192.168.0.1","user_agent":"Moz ...

实际文件得有几百上千条,这么多有循环的办法没,我不会写
lzh68575536 发表于 2020-11-28 15:14
zoenbo 发表于 2020-11-28 15:12
实际文件得有几百上千条,这么多有循环的办法没,我不会写

只要读取文件就行了呗.不用写成$str  就用你原来的
$str= file_get_contents("3.txt");//将文件中的内容读成字符串
$str1=explode("\n",$str);//按换行进行拆分//单引号是转义字符,所以必须使用双引号
即可,如果数据多了的话,可以分段读取
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 10:15

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表