douqinlu4217 2014-01-01 10: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 10: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
    )
    
    评论

报告相同问题?