douran7929 2014-05-20 16:28 采纳率: 100%
浏览 39
已采纳

读取文本文件并显示部分行

I'm creating a hit counter. The script that registers that the visitor was there creates a .txt file like this

 IP Adress:127.0.0.1

 Timestamp:1400602795

 User Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36

 Times visited:1

I want to be able to got information like timestamp and times visited. One way i'd like to do this is to create an array seperated by ":".

I tried to create the array, But for some reason "Times Visited" does not get included in the array. Here is the code from that attempt:

$fileResource = @fopen("hits/".$_SERVER["REMOTE_ADDR"].".txt","r");
$fileContent = @fread($fileResource,100000);

$array = preg_match_all("/(.*):(.*)\
/i",$fileContent,$fileMatches);
//0 = IP
//1 = Timestamp
//2 = User Agent
//3 = Times Visited
echo $fileMatches[0][3];

this is the output when i try print_r($fileMatches)

Array

( [0] => Array ( [0] => IP Adress:127.0.0.1

        [1] => Timestamp:1400604101

        [2] => User Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36

    )

[1] => Array
    (
        [0] => IP Adress
        [1] => Timestamp
        [2] => User Agent
    )

[2] => Array
    (
        [0] => 127.0.0.1
        [1] => 1400604101
        [2] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36
    )

)
  • 写回答

1条回答 默认 最新

  • duansaxf095988 2014-05-20 17:01
    关注

    If you have already read the entire contents of the file into a string, you could use explode twice like this:

    $s = <<<EOT
    IP Address:127.0.0.1
    
    Timestamp:1400602795
    
    User Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36
    
    Times visited:1
    EOT;
    
    $arr = array();
    foreach (explode("
    ", $s) as $line) {    
        if (trim($line) !== '') { // check for empty line
            list($key, $value) = explode(':', $line);   
            $arr[$key] = trim($value);
        }
    }
    print_r($arr);
    

    Which creates an array like this:

    Array ( 
        [IP Address] => 127.0.0.1 
        [Timestamp] => 1400602795 
        [User Agent] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36 
        [Times visited] => 1 
    ) 
    

    Alternatively, instead of using fread you could use fgets to read each line one by one, and replace the foreach loop with something like while (fgets($fileResource))

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多