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
    )
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法