dongshilve4392 2012-06-04 18:25
浏览 7
已采纳

在PHP中爆炸数组值

I'm working on a script that connects to a server using proxies, going through the list until it finds a working proxy.

The list of proxies is like this:

127.0.0.1:8080
127.0.0.1:8080
127.0.0.1:8080
127.0.0.1:8080
127.0.0.1:8080
127.0.0.1:8080
127.0.0.1:8080

Of course, it's not the same IP and port over and over. Now, originally I was just going to use file() to put them all into an array, but that leaves an array with the values including the full line, obviously.

Ideally what I'd like is an array like

"127.0.0.1" => 8080,
"127.0.0.1" => 8080,
"127.0.0.1" => 8080,
"127.0.0.1" => 8080,
"127.0.0.1" => 8080,
"127.0.0.1" => 8080,
"127.0.0.1" => 8080,
"127.0.0.1" => 8080,

But I'm not sure of the easiest (and most efficient) way to do that. Any suggestions?

  • 写回答

2条回答 默认 最新

  • douxuanwei1980 2012-06-04 18:28
    关注

    Loop over the file and do some parsing:

    $path = './file.txt';
    
    $proxies = array();
    foreach(file($path, FILE_SKIP_EMPTY_LINES) as $line) {
      $proxy = trim($line);
      list($ip, $port) = explode(':', $proxy);
    
      $proxies[$ip] = $port;
    }
    
    var_dump($proxies);
    

    Should note that your 'expected' example is invalid array notation as the key is the same for every element. But I just assumed you were going for formatting.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?