douqinlu4217 2014-01-01 02:31
浏览 61

用php读取txt文件并将结果放入数组中

Hi guys i need a guid on read txt file i have a file named mw2.log and this file have mutiple line and per line i have ip:port like as:

1.1.1.1:222
2.2.2.2:3333
3.3.3.3:888
....

with this method i can read file but i want to put every line in array and seprat ip and port and return with $ip and $port result because i want to send ip's and port's throw POST method to other file named add.php

my code is:

<?php

$file = "mw2.log";
$source_file = fopen( $file, "r" ) or die("Couldn't open $file");
if (!feof($source_file)) {
$buffer = fread($source_file, 4096);  // use a buffer of 4KB

//some stuff here

fclose($source_file);
} else {
// error opening the file.
}


//$form_type = 'callofduty4';
//$form_ip = '37.187.71.163';
//$form_c_port = '16044';


$Curl_Session = curl_init('http://127.0.0.1/serverlist/lgsl_files/robot.php');
curl_setopt ($Curl_Session, CURLOPT_POST, 1);
curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "form_type=$form_type&form_ip=$form_ip&form_c_port=$form_c_port");
curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
curl_exec ($Curl_Session);
curl_close ($Curl_Session);

?>

展开全部

  • 写回答

4条回答 默认 最新

  • duanbei3704 2014-01-01 02:38
    关注

    Make use of file() and explode() for this...

    <?php
        $ip_arr=file('mw2.log');
        $ips= array();
        $ports=array();
        foreach($ip_arr as $val)
        {
            $val=explode(':',$val);
            array_push($ips,$val[0]);
            array_push($ports,$val[1]);
        }
     print_r($ips);
     print_r($ports);
    

    OUTPUT :

    Array
    (
        [0] => 1.1.1.1
        [1] => 2.2.2.2
        [2] => 3.3.3.3
    )
    Array
    (
        [0] => 222
    
        [1] => 3333
    
        [2] => 888
    )
    
    评论
  • douyoufan7881 2014-01-01 02:38
    关注

    If you're confident this will be a relatively small and well-formed file, it's probably easier to use file() like so:

    $connectionParams = file("mw2.log");
    /*
    $connectionParams will look like array('1.1.1.1:222', '2.2.2.2:3333', '3.3.3.3:888') at this point
    */
    if ($connectionParams !== false) {
        foreach ($connectionParams as $connectionInfo) {
            // some stuff
        }
    } else {
        // error opening the file
    }
    

    Is this the question you're asking?

    评论
  • doutuo2829 2014-01-01 02:41
    关注

    while (!feof($source_file)) {
        $buffer = fgets($source_file); // read a line    
    
        $parts = explode(':', $buffer); // seperate ip and ports part
        $ip[] = $parts[0];
        $ports[] = $parts[1];
    
    }
    fclose($source_file);
    
    print_r($ip);
    print_r($ports);
    

    $ip has the ip address now and $ports has the ports.

    评论
  • dougai2427 2014-01-01 02:44
    关注

    My take

    $lines = file('file.txt');
    $output = array();
    $count = 0;
    foreach($lines as $v) {
        $parts = explode(":", $v);
        $output[$count]['ip'] = $parts[0];
        $output[$count]['port'] = $parts[1];
        $count++; 
    }
    
    print_r($output);
    
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部